Amazon Interview Question
Software Engineer / DevelopersCountry: India
Interview Type: Written Test
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.
#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));
}
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());
}
}
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
}
}
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)+" ");
}
}
}
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;
}
// 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;
}
// 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);
}
#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;
}
}
Another recursive solution in C#:
- Ian January 23, 2013