Amazon Interview Question


Country: United States




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

public void permutations(String prefix, String s, int size) {
		int len = s.length();
		if (prefix.length() == size) {
			System.out.println(prefix);
		}
		for (int i = 0; i < s.length(); i++)
			permutations(prefix + s.charAt(i),
					s.substring(0, i) + s.substring(i + 1, len), size);
	}

- Dhass April 11, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Function call : permutations("","0123456789", 4)

- Dhass April 11, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Sorry if this is naive, but isn't

s.substring(0, i) + s.substring(i + 1, len)

the same as

s

- Anonymous April 14, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

s.substring(0,i) will give you characters from 0 to i-1. So s.substring(0, i) + s.substring(i + 1, len) will give you string without i character.

- Dhass April 14, 2013 | Flag
Comment hidden because of low score. Click to expand.
1
of 1 vote

In pseudo-code something along the lines of:

public void print(int length)
{
	printHelper(length, "");
}

public void printHelper(int lengthLeft, String strSoFar)
{
	if(lengthLeft == 0)
	{
		cout << strSoFar << endl;
		return;
	}
	for(int i = 0; i <= 9; i++)
	{
		printHelper(lengthLeft-1, strSoFar + toString(i));
	}
}

You keep track of how much longer (how many digits) there is to go and call recursively once for every digit at that position. You also need to pass in the digits so far so you can print em at the end.

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

the first digit cannot be 0 right? shouldn't that be handled as a special case?

- somesh April 11, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

It can be according to the question, it's got 0123 as one of the combinations. But if that weren't legal, it'd be easy to remedy via a boolean flag, or by checking the length of the string so far.

- Anonymous April 11, 2013 | Flag
Comment hidden because of low score. Click to expand.
1
of 1 vote

void printPerm(char *num, int len_so_far,int actual_len)
{
int start,i;
if(len_so_far == actual_len)
{
num[actual_len] = '\0';
printf("%s",num);
}
if(len_so_far ==0 )
start = 1;
else
start = 0;
for(i=start;i<9;i++)
{
num[len_so_far] = itoa(i);
printPerm(num, len_so_far + 1, actual_len);
}

}

- somesh90 April 11, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public static void permutation(int [] target, boolean []used,int length, int level,StringBuffer out){
if( level == length ){
System.out.println( out.toString() );
return;
}

for( int i = 0; i < target.length; ++i ){
if( used[i] ) continue;
out.append( target[i] );
used[i] = true;
permutation( target, used, length, level + 1,out );
used[i] = false;
out.setLength( out.length() - 1 );
}
}

- wy_scott April 11, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public class PrintAllNumberByDigit {
	
	public static void printSequential(int digit, String base) {
		for(int j=0; j < 10; j++) {
			if(base.indexOf(String.valueOf(j)) == -1) {
				String newBase = base + j;
				if(digit > 1) {
					printSequential(digit-1, newBase);
				} else {
					System.out.println(newBase);
				}
			}
		}
	}
	
	public static void printSequential(int digit) {
		printSequential(digit, "");
	}
	
	public static void main(String[] args) {
		printSequential(5);
	}

}

- plzeasy April 12, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

You used permutations and combinations as synonymous in your question, they are not. Google for permutations vs combinations.

- Karthik April 12, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
-1
of 1 vote

yeah, i find problem and fix code

- plzeasy April 12, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

public static void anagram(char a[], int i) {
		if (i == a.length - 1) {
			System.out.print(new String(a)+",");
		} else {
			for(int j=i;j<a.length;j++){
				swap(a, i, j);
				anagram(a, i+1);
				swap(a, i, j);
			}
		}
	}

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

Here is a solution (recursive of course - its really awful to do a permutation problem if one does not user recursion) without string manipulation.

It is in C++. I feel that this would be the answer that would score more brownie points in an interview:

#include <vector>
#include <iostream>

using namespace std;

void permutations(int s, int e, vector<int>& prfx) {
    if (!e) {
        for (int i=0; i < prfx.size(); i++)
            cout << prfx[i];
        cout << endl;
    }

    for (int i=s; i < 10; i++) {
        prfx.push_back(i);
        p(s+1, e-1, prfx);
        prfx.pop_back();
    }
}

int main() {
	vector<int> t;
	permutations(0,4, t);
	return 0;
}

- barry.steyn April 25, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

static void print_permutate(int K){
print_permutate_sub(new boolean[10], 0, K, 0);
}
static void print_permutate_sub(boolean[] used , int num , int K , int d){
if (d > K)
return;
else if (d == K){
System.out.println(num);
return;
}
int base = 0;
if (d == 0)
base = 1;
for (int i = base ; i < 10 ; i++){
if (!used[i]){
used[i] = true;
print_permutate_sub(used , num * 10 + i , K , d+1);
used[i] = false;
}
}
}

- Arian Hosseinzadeh June 09, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

static void print_permutate(int K){
		print_permutate_sub(new boolean[10], 0, K, 0);
	}
	static void print_permutate_sub(boolean[] used , int num , int K , int d){
		if (d > K) 
			return;
		else if (d == K){
			System.out.println(num);
			return;
		}
		int base = 0;
		if (d == 0)
			base = 1;
		for (int i = base ; i < 10 ; i++){
			if (!used[i]){
				used[i] = true;
				print_permutate_sub(used , num * 10 + i , K , d+1);
				used[i] = false;
			}
		}
	}

- Arian Hosseinzadeh June 09, 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