Amazon Interview Question
Software Engineer / DevelopersCountry: United States
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".
#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;
}
Does not work for values more than 4, tried 7 and here's sample o/p "afpdmaf afpdmag afpdmah afpdmai afpdmaj"
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;
}
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);
}
}
}
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));
}
}
}
}
#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;
}
Call func(arr,5,0,1);
- Shiva September 19, 2013