Amazon Interview Question for Software Engineer / Developers


Country: United States




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

Call func(arr,5,0,1);

void func(char arr[],int n,int i,int j)
{
  if(i == n - 1)
  {
    return;
  }
  
  for(k = 0;k< n;k++)
  {
    if(k != i && k != j)
    {
       arr[k] = 'b';
    }
    else
    {
       arr[k] = 'a';
    }
  }

  for(k= n - 1;k >= 0;k--)
  {
   // The bytes initialised with 'a' remain unchanged.

   if(k == i || k == j)
   {
     continue;
   }

   while(arr[k] <= 'z')
   {
     // print the arr

     print(arr);
     ++arr[k];
   }
   
   --arr[k];
  }

  // If second 'a' reaches the end Start moving first 'a'

  if(j == n-1)
  {
    i++;
    j= i+1;
  }
  else
  {
    j++;
  }

  func(arr,n,i,j);
}

- Shiva September 19, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

your code doesn't generate some permutations like "abad" for example when I tried the call func(arr,4,0,1);. I believe this is because you start with the permutation "aabb" and then keep incrementing the last character so it becomes "aabz" and then you increment the second from last character and that's how you miss permutations like "abad".

- Amr Gamal September 21, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

@Amr Gamel : Yes you are right. Thanks for pointing it out.

- Shiva September 21, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

#include <iostream>

using namespace std;
int n;
char * str;

void createstring(char str[],int i)
{
if(i==n)
{
str[n]='\0';
cout<<str<<endl;
return;
}
if(str[i]!='a')
{
for(int j = 1;j<=int ('z'-'a');j++)
{
str[i]=char('a'+j);
createstring(str,i+1);
}
}
else
createstring(str,i+1);
}

int main()
{
cin>>n;
str = new char[n+1];
for(int i = 0; i< n-1; i++)
for(int j = i+1; j< n;j++)
{
str[i] = 'a';
str[j] = 'a';
createstring(str,0);
str[i] = NULL;
str[j] = NULL;
}
return 0;
}

- Satyen September 19, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Does not work for values more than 4, tried 7 and here's sample o/p "afpdmaf afpdmag afpdmah afpdmai afpdmaj"

- Anon - v September 19, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Check carefully... its working pretty good even with 20.

- Satyen September 20, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

void strper(char *str,int a,int n,int l){

int i;

if(l==n){
if(a!=2)return;
str[l]='\0';
printf("%s\n",str);
return;
}
if(a!=2){
str[l]='a';
strper(str,a+1,n,l+1);
}

for(i=1;i<26;i++){

str[l]='a'+i;
strper(str,a,n,l+1);

}
}



int main ( ) {
char *str;
int n;
printf("Enter the value of n:");
scanf("%d",&n)
str=(char *)malloc(n+1);
strper(str,0,n,0);

return 0;
}

- dhamu.31954 September 19, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

No, does now work even for value 4

- Anon -v September 19, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

@Anon....is this comment reffers to my programm

- dhamu.31954 September 20, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

there are two things to think about.
First, we need to generate 6 subsets(if n is 4) 1,2 1,3 1,4 2,3 2,4 3,4
For each subset, we need to make a combination for b-z.
So, if you see the code below, the first subset is 1,2 and the first character in c and d are 'b' and 'b'.
so 1. => b, b, a, a
2 = b, c, a, a
.... and so on.

void subsetperm( int n)
{
for ( int i = 0 ; i < n ; i++)
for ( int j = i; j < n ; j++)
{
for ( char c = 'b'; c <= 'z' ; c++)
for ( char d = 'b'; d <= 'z' ; d++)
{
char* sResult = malloc( n+1);
strcpy ( sResult, "aaaaaaaaaa");
sResult[i] = c;
sResult[j] = d;
printf(sResult);
}
}
}

- moiskim September 19, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public class Sequence {
	public static void main(String[] args) {
		int len = 4;
		String prefix = "aa";
		char[] seq = new char[] { 'b', 'b', 'b' };
		buildSeq(prefix, seq, len - prefix.length());
	}

	private static void buildSeq(String prefix, char[] seq, int col) {
		seq[col] = 'b';
		for (int i = 0; i < 25; i++) {
			seq[col] = (char) ('b' + i);
			if (col != 0) {
				buildSeq(prefix, seq, col - 1);
			} else {
				System.out.println(prefix + new String(seq));
			}
		}
	}
}

- srkavin September 22, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

int len = 4;

is supposed to be

int len = 5;

- srkavin September 22, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

#include <stdio.h>

int check_terminate_condition (int aa1, int aa2, int size, char * array)
{
int retVal = 0;
int i = 0;

for (i = 0; i < size; i++)
{
if (i != aa1 && i != aa2)
{
if (array[i] != 'z')
return 0;
}
}
return 1;
}


int print (int size)
{
int aa1 = 0;
int aa2 = 1;
int i = 0;
char array[10] = {0};

for (i = 0; i < size; i++)
{
if (i == aa1 || i == aa2)
array[i] = 'a';
else
array[i] = 'b';
}

for (i = 0; i < size; i++) printf ("%c", array[i]);
printf ("\n");

while (1)
{
if (check_terminate_condition (aa1, aa2, size, array))
{
array[aa1] = 'b';
array[aa2] = 'b';
if (aa2 == size - 1)
{
if (aa1 == size - 2)
break;
aa1++;
}
else
aa2++;
array[aa1] = 'a';
array[aa2] = 'a';
}

/* increment counter */
for (i = size - 1; i >= 0; i--)
{
if (i != aa1 && i != aa2)
{
if (array[i] == 'z')
{
array[i] = 'b';
}
else
{
array[i]++;
break;
}
}
}
for (i = 0; i < size; i++) printf ("%c", array[i]);
printf ("\n");
}
}


int main (int argc, char *argv)
{
print (5);
return 0;
}

- Vitthal September 23, 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