Amazon Interview Question for Software Engineer / Developers


Country: India
Interview Type: Written Test




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

Another recursive solution in C#:

void PrintCombinations(string letters, int length, string prefix = "")
{
    if (length == 0)
    {
        Console.WriteLine(prefix);
        return;
    }
    
    for (var i = 0; i < letters.Length; i++)
    {
        var newPrefix = prefix + letters[i];
        PrintCombinations(letters, length - 1, newPrefix);
    }
}

- Ian January 23, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Very neat!

- barcod January 23, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

There is no check for length < 0 in the for loop... so when length become -1, and no check is there in the recursive function, so it will again run the iteration.

- aman January 23, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

u r the man ;)

- pras July 15, 2013 | Flag
Comment hidden because of low score. Click to expand.
3
of 3 vote

bestinterviewsquestions.blogspot.in/2013/02/print-all-combination-of-given-length-k.html

- Learn Android: http://learnandroideasily.blogspot.in/ February 06, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
1
of 1 vote

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
char op[4]; // temporary array to store combination
int print(char *s,int k,int n)
{
  int i;
  static int count=0;
  for(i=0;i<strlen(s);i++)
  {
    op[n]=s[i];
    if(n==k-1)
    {
      printf("%s\n",op);
      count++;
    }
    else
      print(s,k,n+1);
  }
  return count;
}
{
   int n;
  printf("\n%d\n",print("abc",2,0));
 
}

- Shashi January 22, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
1
of 1 vote

Generate combinations from bottom up. First of size 1. Then use these to generate combinations of size 2. and so on.

import java.util.LinkedList;
import java.util.List;


public class Test {
	
	public static void main(String[] args) {
		
		System.out.println("hello");
		
		String str = "abcdef";
		
		
		generatePermutations(str,3);
		
	}
	
	public static void generatePermutations(String str,int k){
		
		List permList = new LinkedList<String>();
		for(int j = 0; j < str.length(); j++){
			permList.add(str.charAt(j));
		}
	
		do{
			
			for(int x = 0; x < str.length(); x++){
				String newElement = permList.get(0).toString() + str.charAt(x);
				permList.add(newElement.toString());
			}
			permList.remove(0);
			
		}while(permList.get(0).toString().length() != k);
		
		System.out.println(permList);
		System.out.println("Number of elements : " + permList.size());
		
		
	}

}

- Anonymous January 26, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Thanks !!
I learnt Linked List function in Java thro this program only :)

- Mahesh Babu February 08, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

You can do it by just a two simple nested for over the characters of the string.

- Reza January 23, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

For this recursion is the only soluton as K is variable

- Crazy Tesla January 23, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

can you share other questions as well?

- HardCode January 23, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public static void printComb(String S, int k)
	{
		int ptrs[] = new int[k];		//total of k ptrs that increment from all 0s
							//example 00 to 22 in case of k=2 and S length = 3
		for(int i=0; i<k; i++)			//setting all 0s
			ptrs[i]=0; 
		
		while(ptrs[k-1]!=S.length()){		//till the last counter place is not max
			
			for(int i=k-1; i>=0; i--)
				System.out.print(S.charAt(ptrs[i]));
			
			ptrs[0]++;							//increment units place counter
			
			for(int i=0;i<k;i++){
				if(ptrs[i]==S.length()){		//if any counter has reached limit
					ptrs[i+1]++;				//increment next counter
					for(int j=0;j<=i;j++)		//and reset all previous ones
						ptrs[j]=0;
					break;
				}
			}
				
			System.out.println();			//change line when one word is done
		}
	}

- Mayank January 23, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Simple solution with out using recursion in java:
private void printCombinations(String letters,int length){
for(int i=0;i<letters.length();i++){
for(int j=0;j<letters.length();j++){
System.out.print(""+letters.charAt(i)+letters.charAt(j)+" ");
}
}

}

- Nirdesh MS January 23, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Oh..sorry...i have not consider about the length here....this is only for k=2...

- Nirdesh January 23, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

This will work
public static void main(String args[]){
String s = "abc";

char[] a = s.toCharArray();

for(int i=0;i<a.length;i++){
for(int j=0;j<a.length;j++){

System.out.print(a[i]);
System.out.print(a[j]);
System.out.print("\n");


}
}

}

- Sathishwaran January 24, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

List<String> printcombs (char[] arr, int k) {
		List<String> results = new LinkedList<String>();
		if (k == 1)
		{
			for (int i = 0; i < arr.length; i++)
			{
				results.add(arr[i] + "");
			}
		} 
		else
		{
			List<String> substrings = printcombs(arr, k-1);
			for (int i = 0; i < arr.length; i++)
			{
				for (String substring : substrings)
				{
					results.add(arr[i] + substring);
				}
			}
		}
		return results;
	}

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

// input string tokenized into char array

	List<String> printcombs (char[] arr, int k) {
		List<String> results = new LinkedList<String>();
		if (k == 1)
		{
			for (int i = 0; i < arr.length; i++)
			{
				results.add(arr[i] + "");
			}
		} 
		else
		{
			List<String> substrings = printcombs(arr, k-1);
			for (int i = 0; i < arr.length; i++)
			{
				for (String substring : substrings)
				{
					results.add(arr[i] + substring);
				}
			}
		}
		return results;
	}

- omnomnom January 25, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

// treat S as your set of "digits"
// treak k as the desired length of numbers composed of "digits" from S
// so, we're printing each possible number, for numbers with digits S of length k

void printCombos(char *S, int k)
{
  if(k == 0) return;
  char *buff = malloc(sizeof(char) * (k+1));
  buff[k] = 0x00;
  int base = strlen(S);
  int max = (int)pow((double)base, (double)k);
  int slot, curr;
  for(int i=0; i<max; i++)
  {
    curr = i;
    for(int d=1; d<=k; d++)
    {
      slot = i % base;
      buff[k-d] = S[slot];
      curr -= slot;
      curr /= base;
    }
    printf("%s\n", buff);
  }
  free(buff);
}

- Dillon January 31, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

#include <iostream>
using namespace std;
void permuteRepeat(string, string,int,int);
int main() {
	permuteRepeat("abc","",3,1);
	return 0;
}
void permuteRepeat(string str, string start, int  sublength,  int depth){
	if(depth == sublength){
		for(unsigned j = 0 ; j< str.length(); j++){
			cout<<start<<str[j]<<endl;
		}
	}
	else{
		for(unsigned i = 0 ; i < str.length(); i++){
			string tmp = start + str[i];
			permuteRepeat(str,tmp,sublength,depth+1);

		}
		cout<<endl;
	}
}

- muthu.class February 10, 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