Amazon Interview Question for Software Engineer / Developers


Country: United States
Interview Type: In-Person




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

Lexicografically generated permutations O(n*n!)

boolean nextPerm(int [] nums) 
  {   int i =  nums.length - 2;
	  while(i >= 0) {
			if(nums[i] < nums[i + 1])
				break;
			i--;
	  }
	  if (i ==  -1) {
		  return false;
	  }
	  int j =  nums.length - 1;
	  while(j > i) {
			if(nums[j]>nums[i])
				break;
			j--;
	  }
	  if (j != i) {
		  swap(i,j,nums);
	  }
	  int k = i + 1;
	  int l = nums.length - 1;
	  while(k < l) {
		  swap(k,l,nums);
		  k++;
		  l--;
	  }
	  return true;
		 
  }
void swap(int i, int j, int[] arr) {
		int temp = arr[j];
		arr[j]=arr[i];
		arr[i]=temp;
	}
void generatePermutation(int[] nums) {
	 while (nextPerm(nums)) {
		 for (int i : nums)
			 System.out.print(i);
		 System.out.println();
	 }
  }

- EPavlova January 19, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

the above solution is correct, as it finds the next permutation in linear time with constant space, but first you need to sort array in inceasing order to get the "lowest" value permutation. A heap sort is suitable since it O(1) memory.

- gen-y-s January 20, 2016 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

shall we generate all permutatitions of given list or only one permutation?

- EPavlova January 18, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Sorry, I edited the question for clarity. All permutations

- william.brandon.lee83 January 18, 2016 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

I could reverse the list and this is one permutation, i don't get the point in this exercise.

- EPavlova January 18, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Java Implementation




3.import java.util.*;

4.import java.lang.*;

5.import java.io.*;

6.

7./* Name of the class has to be "Main" only if the class is public. */

8.class Ideone

9.{

10. public static void main (String[] args) throws java.lang.Exception

11. {

12. Integer[] input={1,2,3,4};

13. List<List<Integer>> ans=permute(input);

14. System.out.println(ans.size());

15. for (List<Integer> lstChild : ans){

16. System.out.println();

17.

18. for (Integer lstelement : lstChild)

19. System.out.print(lstelement);

20. }

21.

22. }

23.public static List<List<Integer>> permute(Integer...myInts){

24.

25. if(myInts.length==1){

26. List<Integer> arrayList = new ArrayList<Integer>();

27. arrayList.add(myInts[0]);

28. List<List<Integer> > listOfList = new ArrayList<List<Integer>>();

29. listOfList.add(arrayList);

30. return listOfList;

31. }

32.

33. Set<Integer> setOf = new HashSet<Integer>(Arrays.asList(myInts));

34.

35. List<List<Integer>> listOfLists = new ArrayList<List<Integer>>();

36.

37. for(Integer i: myInts){

38. ArrayList<Integer> arrayList = new ArrayList<Integer>();

39. arrayList.add(i);

40.

41. Set<Integer> setOfCopied = new HashSet<Integer>();

42. setOfCopied.addAll(setOf);

43. setOfCopied.remove(i);

44.

45. Integer[] isttt = new Integer[setOfCopied.size()];

46. setOfCopied.toArray(isttt);

47.

48. List<List<Integer>> permute = permute(isttt);

49. Iterator<List<Integer>> iterator = permute.iterator();

50. while (iterator.hasNext()) {

51. List<java.lang.Integer> list = iterator.next();

52. list.add(i);

53. listOfLists.add(list);

54.

55. }

56. }

57.

58.return listOfLists;

59. }

60.}

- Anonymous January 18, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

3.import java.util.*;

4.import java.lang.*;

5.import java.io.*;

6. 

7./* Name of the class has to be "Main" only if the class is public. */

8.class Ideone

9.{

10.	public static void main (String[] args) throws java.lang.Exception

11.	{

12.		Integer[] input={1,2,3,4};

13.		List<List<Integer>> ans=permute(input);

14.		System.out.println(ans.size());

15.		for (List<Integer> lstChild : ans){

16.			System.out.println();

17. 

18.			for (Integer lstelement : lstChild)

19.			System.out.print(lstelement);

20.		}

21. 

22.	}

23.public static List<List<Integer>> permute(Integer...myInts){

24. 

25.        if(myInts.length==1){

26.            List<Integer> arrayList = new ArrayList<Integer>();

27.            arrayList.add(myInts[0]);

28.            List<List<Integer> > listOfList = new ArrayList<List<Integer>>();

29.            listOfList.add(arrayList);

30.            return listOfList;

31.        }

32. 

33.        Set<Integer> setOf = new HashSet<Integer>(Arrays.asList(myInts));   

34. 

35.        List<List<Integer>> listOfLists = new ArrayList<List<Integer>>();

36. 

37.        for(Integer i: myInts){

38.            ArrayList<Integer> arrayList = new ArrayList<Integer>();

39.            arrayList.add(i);

40. 

41.            Set<Integer> setOfCopied = new HashSet<Integer>();

42.            setOfCopied.addAll(setOf);

43.            setOfCopied.remove(i);

44. 

45.            Integer[] isttt = new Integer[setOfCopied.size()];

46.            setOfCopied.toArray(isttt);

47. 

48.            List<List<Integer>> permute = permute(isttt);

49.            Iterator<List<Integer>> iterator = permute.iterator();

50.            while (iterator.hasNext()) {

51.                List<java.lang.Integer> list = iterator.next();

52.                list.add(i);

53.                listOfLists.add(list);

54. 

55.            }

56.        }   

57. 

58.return listOfLists;

59.	}

60.}

- Anonymous January 18, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

void printPerm(nums, s, p){
		if( s == -1 || s == p) return;
		if(p < nums.length) {
			//swap
			tmp = nums[p];
			nums[p] = nums[p-1];
			nums[p-1] = tmp;
		}
		for (num : nums) {
			print num + " ";
		}
		print "\n"
		printPerm(num, p, num.length -1);
		p -= 1;
		printPerm(num, s, p);

	}

	void main(...){
		printPerm(nums, 0, nums.length)
	}

- vinh May 10, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

void printPerm(nums, s, p){
		if( s == -1 || s == p) return;
		if(p < nums.length) {
			//swap
			tmp = nums[p];
			nums[p] = nums[p-1];
			nums[p-1] = tmp;
		}
		for (num : nums) {
			print num + " ";
		}
		print "\n"
		printPerm(num, p, num.length -1);
		p -= 1;
		printPerm(num, s, p);

	}

	void main(...){
		printPerm(nums, 0, nums.length)
	}

- vinh May 10, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

void printPerm(nums, s, p){
		if( s == -1 || s == p) return;
		if(p < nums.length) {
			//swap
			tmp = nums[p];
			nums[p] = nums[p-1];
			nums[p-1] = tmp;
		}
		for (num : nums) {
			print num + " ";
		}
		print "\n"
		printPerm(num, p, num.length -1);
		p -= 1;
		printPerm(num, s, p);

	}

	void main(...){
		printPerm(nums, 0, nums.length)
	}

- vinh May 10, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

void printPerm(nums, s, p){
if( s == -1 || s == p) return;
if(p < nums.length) {
//swap
tmp = nums[p];
nums[p] = nums[p-1];
nums[p-1] = tmp;
}
for (num : nums) {
print num + " ";
}
print "\n"
printPerm(num, p, num.length -1);
p -= 1;
printPerm(num, s, p);

}

void main(...){
printPerm(nums, 0, nums.length)
}

- sdfsdf May 10, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public void permutation(int[] array) {
        int size = factorial(array.length);
        for (int i = 0; i < size; i++) {
            int i1 = i % array.length;
            int i2 = (i + 1) % array.length;
            int tmp = array[i1];
            array[i1] = array[i2];
            array[i2] = tmp;
            printArray(array);
        }}
public static int factorial(int value) {
        if (value == 1) {
            return 1;
        }
        return value * factorial(value - 1);

}

- Bintoo July 07, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
-1
of 1 vote

One random permutation?

private static void permutate(int arr[], int n) {
        Random rand = new Random();
        for(int i = n - 1; i > 0; --i) {
            int j = rand.nextInt(i + 1);
            int temp = arr[j];
            arr[j] = arr[i];
            arr[i] = temp;
        }
    }

- Shay January 18, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Sorry, I edited the question for clarity. All permutations

- william.brandon.lee83 January 18, 2016 | Flag
Comment hidden because of low score. Click to expand.
-1
of 1 vote

One random permutation?

private static void permutate(int arr[], int n) {
        Random rand = new Random();
        for(int i = n - 1; i > 0; --i) {
            int j = rand.nextInt(i + 1);
            int temp = arr[j];
            arr[j] = arr[i];
            arr[i] = temp;
        }

}

- Shay January 18, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
-1
of 1 vote

private static void permutate(int arr[], int n) {
        Random rand = new Random();
        for(int i = n - 1; i > 0; --i) {
            int j = rand.nextInt(i + 1);
            int temp = arr[j];
            arr[j] = arr[i];
            arr[i] = temp;
        }

}

- Shay January 18, 2016 | 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