Amazon Interview Question
SDE-2sTeam: Payments
Country: India
Interview Type: In-Person
O(n) solution
// Returns length of the longest contiguous subsequence
int findLongestConseqSubseq(int arr[], int n)
{
unordered_set<int> S;
int ans = 0;
// Hash all the array elements
for (int i = 0; i < n; i++)
S.insert(arr[i]);
// check each possible sequence from the start
// then update optimal length
for (int i=0; i<n; i++)
{
// if current element is the starting
// element of a sequence
if (S.find(arr[i]-1) == S.end())
{
// Then check for next elements in the
// sequence
int j = arr[i];
while (S.find(j) != S.end())
j++;
// update optimal length if this length
// is more
ans = max(ans, j - arr[i]);
}
}
return ans;
}
public class Q6 {
public static void main(String[] args) {
int array[]={1,94,93,1000,2,92,1001};
array=mergeSort(array);
int last_ele=array[0];
int i=1,count=1,max=1;
while(i<array.length)
{
if(array[i]==last_ele+1)
{
count++;
}
else
{
if(max<count)
max=count;
count=1;
}
last_ele=array[i++];
}
System.out.println("max consecutive numbers : "+max);
}
private static int[] mergeSort(int[] array) {
// TODO Auto-generated method stub
if(array.length<=1)
return array;
int mid=array.length/2;
int temp1[]=new int[mid];
for(int i=0;i<mid;i++)
temp1[i]=array[i];
int temp2[]=new int[array.length-mid];
for(int i=mid;i<array.length;i++)
temp2[i-mid]=array[i];
temp1=mergeSort(temp1);
temp2=mergeSort(temp2);
array=mergeArray(temp1, temp2);
return array;
}
private static int[] mergeArray(int[] array1, int[] array2) {
// TODO Auto-generated method stub
int result[]=new int[array1.length+array2.length];
int i=0,j=0;
while(i<array1.length&&j<array2.length)
{
if(array1[i]<=array2[j])
{
result[i+j]=array1[i];
++i;
}
else
{
result[i+j]=array2[j];
++j;
}
}
while(j<array2.length)
{
result[i+j]=array2[j];
++j;
}
while(i<array1.length)
{
result[i+j]=array1[i];
++i;
}
return result;
}
}
O(n) solution (ignoring sorting)
int maxLenConsNums(vector<int>& arr){
if(arr.size() <= 1) return arr.size();
sort(arr.begin(), arr.end());
int maxLen = 0, currLen = 0;
int i = 1;
while(i < arr.size()){
int diff = arr[i] - arr[i-1];
if(diff == 1){
currLen++;
}
else if(diff != 1){
maxLen = max(maxLen, currLen);
currLen = 0;
}
i++;
}
return maxLen+1;
}
int maxConsecutiveLen(vector<int>& arr){
map<int,int> pos;
for(int i = 0; i < arr.size(); i++){
pos[arr[i]] = i;
}
int maxcount(0), count(0);
auto p = pos.begin();
for(auto i = pos.begin(); i != pos.end(); i++){
if(i->first - p->first == 1){
count++;
}
else{
maxcount = max(maxcount, count);
count = 0;
}
p = i;
}
return maxcount+1;
}
#include<iostream.h>
using namespace std;
int main()
{
int n;
int max=1;
cin>>n;
int temp=0;
int arr[n];
for(int i=0; i<n; i++)
{
cin>>arr[i];
}
for(int j=0; j<n; j++)
{
for(int k=j; k<n; k++)
{
if(arr[j]>arr[k])
{
temp=arr[j];
arr[j]=arr[k];
arr[k]=temp;
}
}
}
int x;
for(int l=0; l<n; l++)
{
int ct=1;
x=arr[l];
for(int m=0; m<n-1; m++)
{
if(arr[m+1]==(x+1))
{
ct++;
x=x+1;
}
}
if(ct>max)
{
max=ct;
}
}
cout<<max<<endl;
}
What about the following approach
def findMaximumNumberinJumbled(listOfNumbers):
countyet = 1
countMax = 1
sortedList = sorted(listOfNumbers)
print sortedList
for index in range(1, len(sortedList)):
if sortedList[index] - sortedList[index-1] != 1:
countyet = 1
elif sortedList[index] - sortedList[index-1] == 1:
countyet = countyet +1
if countyet > countMax:
countMax = countyet
print countMax
if __name__ == "__main__":
findMaximumNumberinJumbled([1,94,93,1000,2,92,1001,1002,999,3,4,5,6])
Sorting Solution:
Time Complexity = O(nlogn)
Space Complexity = O(1)
HashSet Solution:
Time Complexity = O(n) (amortized)
Space Complexity = O(n)
There is a trade-off for both the solutions.
Implemented both of them below:
- settyblue July 26, 2016