## Amazon Interview Question for SDE1s

• 1
of 1 vote

Country: United States

Comment hidden because of low score. Click to expand.
0
of 0 vote

High level steps:

a. Sort array
b. Remove duplicates
c. For every step, check if the value of {CurrentElement + 5} - {CurrElement} = 5
Following is c++ code:

``````int myarray[] = { 1,2,10,11,3,12,13, 4,4,4,5,9,8,6 };

//Step 1 - Sort
std::qsort(myarray, 14, sizeof(int), [](const void* a, const void* b) { return *( (int*) a) -
*( (int *) b );});

//Step 2 - Remove duplicates by putting in set
std::set<int> myset;
for (int &m : myarray)
{
myset.insert(m);
}
//Step 3 - Check for patterns
auto iter = myset.begin();
int i = 0;

while (iter != myset.end() && i < 5)
{
++iter;
++i;

}
if (iter == myset.end() && i != 4)
{
std::cout << " No combinations exist" << endl;
}
else
{
auto beginiter = myset.cbegin();
while (iter != myset.end())
{
if (*iter - *beginiter == 5)
{
std::cout << "Pattern exists between " << *beginiter << " & " << *iter
<< endl;
}
++iter;
++beginiter;
}
}``````

Comment hidden because of low score. Click to expand.
0
of 0 vote

How does the range matter on the split? The given size of the original list is what matters right? And i dont see anywhere in the question that you are allowed to remove duplicates. Is that an assumption? Its a bit confusing. Can the original poster please provide a couple more examples of inputs / outputs?

Thank you

Comment hidden because of low score. Click to expand.
0
of 0 vote

``````first = ip
first_list = []
first_list.append(first)
second_list = []
second_list_offset = 0

for each in ip[1:]:
if each == first+1 and len(first_list) < 5:
first_list.append(each)
first = each
else:
if second_list_offset !=0:
if each != second_list[second_list_offset-1] +1 :
print("Not possible")

if second_add is True and len(second_list) < 5:
second_list.append(each)
second_list_offset+=1
else:
print("Not possible")``````

Comment hidden because of low score. Click to expand.
0
of 0 vote

collect only distinct elements and then sort the array.
Complexity : O(nlogn) time and O(n) space

``````i, count = 1, 1
while(i < len(nums)):
if(A[i-1] == A[i]-1):
count += 1
if(count == 5) return true
else:
count = 1
return false``````

Name:

Writing Code? Surround your code with {{{ and }}} to preserve whitespace.

### Books

is a comprehensive book on getting a job at a top tech company, while focuses on dev interviews and does this for PMs.

### Videos

CareerCup's interview videos give you a real-life look at technical interviews. In these unscripted videos, watch how other candidates handle tough questions and how the interviewer thinks about their performance.