Amazon Interview Question for SDE1s


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;
		}
	}

- ITWala May 03, 2018 | Flag Reply
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

- Jay May 03, 2018 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

first = ip[0]
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:
        second_add = True
        if second_list_offset !=0:
            if each != second_list[second_list_offset-1] +1 :
                print("Not possible")
                second_add = False

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

- SearchingFor! May 09, 2018 | Flag Reply
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

- ashishgopalhattimare July 08, 2019 | Flag Reply


Add a Comment
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.

Learn More

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.

Learn More

Resume Review

Most engineers make critical mistakes on their resumes -- we can fix your resume with our custom resume review service. And, we use fellow engineers as our resume reviewers, so you can be sure that we "get" what you're saying.

Learn More

Mock Interviews

Our Mock Interviews will be conducted "in character" just like a real interview, and can focus on whatever topics you want. All our interviewers have worked for Microsoft, Google or Amazon, you know you'll get a true-to-life experience.

Learn More