Reverse a Linked List

Objective: Reverse the given linked list.

Input: A Linked List

Output: Reversed Linked List

Example:

Input : ->30->25->20->15->10->5

Reversed : ->5->10->15->20->25->30

NOTE : Click Reverse a Linked List – Part 2 to see the another implementation of this problem.

Approach:

Read moreReverse a Linked List

Find a pair of numbers from an array whose sum equals k

Objective: Write an algorithm to find out whether in a given array there exists or not two numbers whose sum is exactly equals to a given number. This problem has been asked in Amazon and Microsoft interviews.

Input: An array arrA[], A number k

Output: True or false based upon we have found any two numbers in array arrA[] whose sum is equal to k

Approach:

Method 1: Using Binary Search

Read moreFind a pair of numbers from an array whose sum equals k

Check If String has All Unique Characters

Objective: Write an algorithm to find out whether in a given string contains all the unique characters. This question has been asked in the Amazon and Microsoft interviews.

Input:  A String

Output: True or false based upon whether string contains all the unique characters or not.

Approach:

Method 1.

Read moreCheck If String has All Unique Characters

Find Whether Given String is palindrome or Not.

Objective : Write an algorithm to find Whether Given String is palindrome or Not.

Input:  A String,

Output: true or false on whether string is palindrome or not

Approach:

  • Use recursive approach
  • Compare first and last characters if they are not same- return false
  • If they are same make, remove the first and last characters and make a recursive call. 

Example:

Jain niaJ => compare ‘J’ with ‘J’ =>returns true

ain nia => compare ‘a’ with ‘a’ =>returns true

in ni => compare ‘i’ with ‘i’ =>returns true

n n => compare ‘n’ with ‘n’ =>returns true

string length <2 => returns true

Read moreFind Whether Given String is palindrome or Not.

Find a peak element in a Given Array

Objective : In this article we will discuss an algorithm to Find a peak element in a Given Array. We will see the recursion techniques to solve this problem.

Peak Element: peak element is the element which is greater than or equal to both of its neighbors.

Input:  Array, arrA[] .

Output: A peak element and its index

Approach:

A simple approach is to do a linear scan to a array and using few variables we can find a peak element. But the Time Complexity will be O(n) but real question is, Can we do better?

The Answer is yes, by using Binary Search techniques.

Read moreFind a peak element in a Given Array

Find two Missing Numbers in a Sequence of Consecutive Numbers

Objective : Write an algorithm to find two Missing Numbers in a Sequence of Consecutive Numbers

Input:  Array, arrA[] with two missing numbers and Range

Output : Two missing numbers

Approach:

    • Approach is very simple, Add all the given numbers say S
    • Calculate sum of N numbers by formula n(n+1)/2 , say N
    • Find sum of two missing numbers a+b = N-S

Read moreFind two Missing Numbers in a Sequence of Consecutive Numbers

Find a Missing Number From a Sequence of Consecutive Numbers

Objective : You have been asked to Write an algorithm Find a Missing Number From a Sequence of Consecutive Numbers

Input:  Array, arrA[] with a missing number and Range

Output : missing number

Approach:

  • Approach is very simple, Add all the given numbers say S
  • Calculate sum of N numbers by formula n(n+1)/2 , say N
  • Find missing number m = N-S

Read moreFind a Missing Number From a Sequence of Consecutive Numbers