AfterAcademy Tech
•
04 Oct 2019

Difficulty: Hard
Asked in: Amazon, Google
Problem Description: Given an unsorted array A[] consisting of n integers, you need to find the length of the longest consecutive sequence of integers in the array.
Example 1
Input: A[] = [10, 4, 20, 1, 2, 8, 9, 3, 19]
Output: 4
Explanation: The longest consecutive sequence of integers in the array is 1,2,3 and 4
Example 2
Input: A[] = [0, -2, 3, -1, 2, 1]
Output: 6
Explanation: The longest consecutive sequence of integers in the array is -2,-1,0,1,2, and 3.
Possible follow-up questions to ask the interviewer :
We will be discussing three ways to solve the problem:-
For each element in A[], linearly search for consecutive elements greater and lesser than the current element. Keep a track of current streak and the longest streak of consecutive elements in the array.
Solution Steps
Pseudo-Code
int longestConsecutiveSequence(int A[], int n)
{
int longest_streak = 0
for( i = 0 to n-1 )
{
int curr_num = A[i]-1
int curr_streak = 1
while( linear_search(A,n,curr_num) == True )
{
curr_streak = curr_streak + 1
curr_num = curr_num - 1
}
curr_num = A[i]+1
while( linear_search(A,n,curr_num) == True )
{
curr_streak = curr_streak + 1
curr_num = curr_num + 1
}
longest_streak = max(longest_streak, curr_streak)
}
return longest_streak
}
Complexity Analysis
The inner loops linearly search for consecutive elements and the worst-case time complexity for linear search is O(n). This search will be done as long as the streak will be and the longest a streak can be is the size of the array, i.e., n. Therefore, inside the outer loop, the worst-case time complexity is O(n²).
Also, this process is repeated for each element of the array. So time Complexity = n * O(n²) = O(n³)
Space Complexity: O(1)
Critical ideas to think!
Sort the entire array A[]. Now, the consecutive elements will be linearly lined-up next to each other. Linearly check for the longest consecutive sequence now.
Pseudo-Code
int longestConsecutiveSequence(int A[], int n)
{
int longest_streak = 0
sort(A, n)
i = 0
while(i < n)
{
int curr_streak = 1
while(i < n and A[i+1] == A[i] + 1)
{
curr_streak = curr_streak + 1
i = i + 1
}
if ( curr_streak == 1 )
i = i + 1
longest_streak = max(longest_streak, curr_streak)
}
return longest_streak
}
Complexity Analysis
Time Complexity: Sorting an array + Linear Traversal of the array (Even though nested loops exist, both loops use same loop variable which is increased linearly) = O(nlogn) + O(n) = O(nlogn)
Space Complexity: If we use Merge Sort, O(n), else if we use Heap Sort, O(1)
Critical ideas to think!
We can improve the time complexity of searching consecutive elements by using a hash table which can check the presence of consecutive elements in O(1) average.
Solution Steps
4. Update longest_streak if curr_streak is bigger than it.
5. Return longest_streak.
Pseudo-Code
int longestConsecutiveSequence(int A[], int n)
{
int longest_streak = 0
Create HashTable H of size n
for( i = 0 to n-1 )
H.add(A[i])
for( i = 0 to n-1 )
{
// This checks if the current element is the first
// element of a sequence
if( H.search(A[i]-1) == False )
{
int curr_streak = 1
int curr_num = A[i]+1
while( H.search(A, curr_num) == True )
{
curr_streak = curr_streak + 1
curr_num = curr_num + 1
}
longest_streak = max(longest_streak, curr_streak)
}
}
return longest_streak
}
Complexity Analysis
Since the time complexity of the search operation is now O(1), we will just need to check how many times that operation will be performed. You can see that each element is searched at most two times. First, in the if condition and second, in the while loop condition. So the complexity due to this is linear ( O(n) ) in nature.
Since each element is visited only once, Time Complexity: O(n)
Space Complexity: O(n), for the Hash Table
Critical ideas to think!

Happy Coding! Enjoy Algorithms!
AfterAcademy Tech
You are given an array A with N elements. You need to find the longest increasing subsequence in the array.

AfterAcademy Tech
This is a mathematical problem asked in interviews of Google like companies. For solving such problems you need to have some math skills and also some idea of algorithms like Euclid's GCD algorithm.

AfterAcademy Tech
This is an interview problem asked in companies like Google, Microsoft, Amazon. The problem is to remove duplicates from a sorted array or list.

AfterAcademy Tech
This is an interview problem asked in companies like Amazon, Microsoft. The problem is to merge two binary trees. The overlapping nodes are replaced by the sum of corresponding nodes. We are discussing two ways of solving this.
