Amazon Interview Question for Software Engineer / Developers


Country: India
Interview Type: Written Test




Comment hidden because of low score. Click to expand.
3
of 7 vote

1)first sorting the list
2)traversing the list and return the min absolute pair.
the time complexity is O(nlogn).
I do not know if there is any solution in O(n) steps.

- Jason November 30, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
2
of 2 votes

Lookup element distinctness problem and you will get an answer for your last sentence.

- Subbu. November 30, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Subbu - I don't get how the element distinctness problem helps solve this question , can you elaborate? Thanks.

- Manoj December 01, 2013 | Flag
Comment hidden because of low score. Click to expand.
1
of 1 vote

@Jason: there is no O(n) solution for this as subbu mentioned by referring you to 'element distinctness problem'.This problem is just to prove the point that there can't be O(n) solution possible for this problem.

- aka December 02, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

@aka, @Subbu, thank you for the proof.

- Jason December 02, 2013 | Flag
Comment hidden because of low score. Click to expand.
3
of 3 votes

O(nlgn) solution:

public static List<Pair> getMinDiff(int[] A)
{
	ArrayList<Pair> pairs;
	int minDiff = Integer.MAX;
	
        A=quicksort(A);
	for(int i=0;i<A.length-1;i++)
	{
		if (abs(A[i+1]-A[i]) < minDiff)
		{
			pairs = new ArrayList<Pair>();
			pairs.add(new Pair(A[i], A[i+1]));
			minDiff = abs(A[i+1]-A[i]) ;
		}
		else if (abs(A[i+1]-A[i]) == minDiff)
		{
			pairs.add(new Pair(A[i], A[i+1]));
		}
	}

	return pairs;
}

- zahidbuet106 December 10, 2013 | Flag
Comment hidden because of low score. Click to expand.
2
of 2 vote

O(nlgn) with O(1) space solution
1. Sort the array.
2. Scan the sorted array from left to right. keep minDiff to contain the min difference between two elements during the scan.
3. If diff between two consecutive is less than minDiff so far then create a list and add the pair.
4. if diff between two consecutive is equal to minDiff that means there is already a list. Add the pair to the list.

public static List<Pair> getMinDiff(int[] A)
{
	ArrayList<Pair> pairs;
	int minDiff = Integer.MAX;
	
   A=quicksort(A);
	for(int i=0;i<A.length-1;i++)
	{
		if (abs(A[i+1]-A[i]) < minDiff)
		{
			pairs = new ArrayList<Pair>();
			pairs.add(new Pair(A[i], A[i+1]));
			minDiff = abs(A[i+1]-A[i]) ;
		}
		else if (abs(A[i+1]-A[i]) == minDiff)
		{
			pairs.add(new Pair(A[i], A[i+1]));
		}
	}

	return pairs;
}

- zahidbuet106 December 05, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

have you ever tried to run your code? so poorly written!

- roman October 11, 2015 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Ambiguous question.

If array is all 1. Do we print n(n-1)/2 pairs? or just 1?

- Anonymous November 30, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Using this method we can find the absolute minimum difference:

Assume numbers are array a[ ].

for(int i=1;i<(sizeof(a)/sizeof(int));i++)
    {
         min=a[1]-a[0];
         max=a[0];
         if((a[i]-min)<min)
         {
            min=a[i]-max;
            start=a[i];
            end=max;
         }
         if(a[i]>max)
         max=a[i];

}

after finding absolute min we can get the elements using start and end.

but since we need to find all. After getting the absolute minimum we will traverse the liist and find out two numbers having difference equal to absolute min.

- Nascent November 30, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

I agree with Jason. First sort the list, then compute the min of

a[i]-a[i-1]
for i = 1 to n-1

where a is the array of numbers (sorted).

- jarflores December 01, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

1> Convert all numbers as positive integers.
2> Sort them. complexity O(n log n) Average/best time
3> Traverse once to find the closest two adjacent numbers. O(n)
Overall complexity O(n log n)

- code December 01, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Kindly Ignore this.. not going to work with -20,30

- Anonymous December 01, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Sorry, above won't work for -20,30

- Code December 01, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Solution written in C++. Time complexity O(nlogn).

Output:

-470 - -520 = 50
30 - -20 = 50

Code:

#include <iostream>
#include <vector>
#include <algorithm>
#include <limits>

std::vector<std::pair<int, int>> find_min_abs_diff(std::vector<int>& arr) {
	// Sort the vector O(nlogn)
	std::sort(arr.begin(), arr.end());
	
	// Find the difference between every adjacent elements. O(n)
	// Keep track of minimum
	int min_so_far = std::numeric_limits<int>::max();
	std::vector<std::pair<int, int>> pairs;
	
	for (size_t i = 1; i < arr.size(); i++) {
		int diff = std::abs(arr[i] - arr[i-1]);
		if (diff <= min_so_far) {
			min_so_far = diff;
			pairs.emplace_back(arr[i], arr[i-1]);
		}
	}
	
	// Remove elements that are greater than the minimum found.
	pairs.erase(std::remove_if(pairs.begin(), pairs.end(),
		[&](const std::pair<int, int>& p) {
			return p.first - p.second > min_so_far;
		}), pairs.end());
	
	return pairs;
}

int main() {
	std::vector<int> arr{-20, -3916237, -357920, -3620601, 7374819, -7330761,
						 30, 6246457, -6461594, 266854, -520, -470};
	std::vector<std::pair<int, int>> min_pairs = find_min_abs_diff(arr);
	for (const auto& p: min_pairs) {
		std::cout << p.first << " - " << p.second << " = ";
		std::cout << p.first - p.second << std::endl;
	}
}

- Diego Giagio December 01, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

In c#, maybe not the most elegant, but this should work:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace InterviewQuestions
{
    struct pair
    {
        public int first;
        public int second;
    }

    class Program
    {
        static void Main(string[] args)
        {
            int[] arr = { -20, -3916237, -357920, -3620601, 7374819, -7330761, 30, 6246457, -6461594, 266854, -520, -470 };
            int nSmallest = Int32.MaxValue;
            List<pair> results = new List<pair>();


            for (int i = 0; i < arr.Length; i++)
            {
                for (int j = i+1; j < arr.Length; j++)
                {

                    if (Math.Abs(arr[i] - arr[j]) < nSmallest)
                    {
                        results.Clear();
                        pair p;
                        p.first = arr[i];
                        p.second = arr[j];
                        results.Add(p);
                        nSmallest = Math.Abs(arr[i] - arr[j]);
                    }
                    else if (Math.Abs(arr[i] - arr[j]) == nSmallest)
                    {
                        results.Add(new pair { first = arr[i], second = arr[j] });
                    }
                }
            }

            foreach (pair r in results)
                Console.WriteLine("({0},{1})",r.first,r.second);

            Console.ReadLine();
        }
    }
}

- TC December 02, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

here my solution in O(n log n) time.
@Diego: i think this might be faster than your algo...since i dont call vector.erase...which allocates the vector new?

#include <iostream>
#include <map>
#include <vector>
#include <algorithm>
using namespace std;

typedef struct mypair {
    int x;
	int y;
	mypair(int x_, int y_): x(x_), y(y_) {}
}mypair;

vector<mypair> get_numbers_min_diff(vector<int> &vec) {
	map<int,vector<mypair>> m;

	// O(n log n)
	sort(vec.begin(), vec.end());
	
	// O(n log n)
	for(int i = 0; i < vec.size() - 1; i++) {
		int diff = vec[i+1] - vec[i];

		auto it = m.find(diff);

		if(it == m.end()) {
			vector<mypair> v;
			v.push_back(mypair(vec[i], vec[i+1]));
			m.insert(make_pair(diff, v));
		}
		else {
			it->second.push_back(mypair(vec[i], vec[i+1]));
		}
	}

	auto it = m.begin()->second;
	
	return it;
}

int main() {
	vector<int> vec = {-20, -3916237, -357920, -3620601, 7374819, -7330761, 30, 6246457, -6461594, 266854, -520, -470};

	vector<mypair> sp = get_numbers_min_diff(vec);

	for_each(sp.begin(), sp.end(), [](mypair p) { cout << p.x << ", " << p.y << endl; });


	return 0;
}

- Gerald December 04, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

#include<stdio.h>
#include<math.h>
#define MAX 100
int main()
{
int arr[12]={-20,-3916237,-357920,-3620601,7374819,-7330761,30,6246457,-6461594,266854,-520,-470};
int i,j,temp,c[11],min;
/* Sort the array*/
for (i= 0;i<11;i++)
{
for (j=0;j<(12-i-1);j++)
{
if (arr[j]>arr[j+1])
{
temp =arr[j];
arr[j]=arr[j+1];
arr[j+1]=temp;
}}}

for(i=0;i<11;i++)
{
c[i]=abs(arr[i]-arr[i+1]);

}
min=c[i];
for(i=0;i<11;i++)
{
if(min>c[i])
min=c[i];

}
for(i=0;i<11;i++)
{
if(min==c[i])
printf("%d %d",arr[i],arr[i+1]);
}


return 0;
}

- harshit1230 December 06, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote
This simple python solution is generic in that it will work for any two or single unsorted array... {{{ from numpy import * def min_diff(a, b): x = array(zip(kron(a, ones(len(b))),kron(ones(len(a)), b))) x = delete(x, where(x[:,0]==x[:, 1]), 0) return int(min(abs((x[:, 0]-x[:,1])))) }} - aykut December 06, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

1.) Sort the list;
2.) Traverse the list twice: first to find the min difference and second to find all pairs with the min difference found

public String displaySmallestDifference(List<Integer> numbers) {
		String smallestDifferences = "";
		
		Collections.sort(numbers);

		Integer smallestDifference = null;
		Integer currentDifference = null;
		
		for(int i = 0; i < numbers.size(); i++) {
			if(i + 1 < numbers.size()) {
				currentDifference = Math.abs(numbers.get(i+1) - numbers.get(i));
				if(smallestDifference == null || currentDifference <= smallestDifference) {
					smallestDifference = currentDifference;
				}
			}
		}
		
		for(int i = 0; i < numbers.size(); i++) {
			if(i + 1 < numbers.size() && 
					Math.abs(numbers.get(i + 1) - numbers.get(i)) == smallestDifference) {
				smallestDifferences += " "+numbers.get(i)+" "+numbers.get(i + 1);
			}
		}
		
		return smallestDifferences;
	}

- Fernando December 11, 2013 | 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