Amazon Interview Question
Software Engineer / DevelopersCountry: India
Interview Type: In-Person
str is the string combination, k is start from 0, n is the string length
void findComp(char* str, int k, int n){
if(k == n-1){
str[k] = 'p';
str[n] = '\0';
printf("str=%s\n", str);
str[k] = 'o';
str[n] = '\0';
printf("str=%s\n", str);
}else{
str[k] = 'p';
findComp(str, k+1, n);
str[k] = 'o';
findComp(str, k+1, n);
}
}
public class Main {
public static void main(String[] args) {
combinations("hello", "", 3);
}
private static void combinations(String sourceString, String accum,
int limit) {
if (sourceString == null || sourceString.length() == 0
|| "".equals(sourceString) || limit == 0) {
System.out.println(accum);
} else {
for (char ch : sourceString.toCharArray()) {
combinations(sourceString, accum + ch, limit - 1);
}
}
}
}
public void printString(List chars, int reqLength, int currentLength, StringBuilder stbd){
if(reqLength == currentLength)
{
System.out.println(stbd);
return;
}
Iterator itr = chars.iterator();
while(itr.hasNext()){
stbd.append(((Character)itr.next()).charValue());
printString(chars, reqLength, currentLength+1, stbd);
stbd.delete(stbd.length()-1);
}
}
<pre lang="" line="1" title="CodeMonkey35868" class="run-this">private static void GetAllPermutations(char[] tokens, int len, ref HashSet<string> words)
{
if (len == 0)
{
return;
}
words = new HashSet<string>();
for (int i = 0; i < tokens.Length; i++)
{
words.Add("" + tokens[i].ToString());
}
HashSet<string> lastSet = words;
for(int size = 2; size <= len; size++)
{
HashSet<string> currentSet = new HashSet<string>();
foreach (var word in lastSet)
{
for (int i = 0; i < tokens.Length; i++)
{
char currentChar = tokens[i];
for (int k = 0; k <= word.Length; k++)
{
currentSet.Add(word.Insert(k, currentChar.ToString()));
}
}
}
lastSet = currentSet;
words = currentSet;
}
}</pre><pre title="CodeMonkey35868" input="yes">
</pre>
// 'letters' is an array of letters
List<string> GetWords(char[] letters, int length)
{
List<string> currentSet = new List<string>();
List<string> nextSet = new List<string>();
List<string> tempSet;
for (int l = 0; l < letterCount; l++)
{
currentSet.Add(string.Empty);
}
for (int x = 0; x < length; x++)
{
foreach(string item in currentSet)
{
for (int l = 0; l < letterCount; l++)
{
nextSet.Add(item + letters[l]);
}
}
currentSet.Clear();
tempSet = currentSet;
currentSet = nextSet;
nextSet = tempSet;
}
return currentSet;
}
#include <stdio.h>
#include <math.h>
#define LENGTH 3
#define CHARACTERS 2
void convert_to_binary(int a[],int i) {
int j = LENGTH - 1;
for (j = LENGTH - 1; j >= 0; --j) {
a[j] = i%2;
i = i/2;
if (i == 0) break;
}
}
int main() {
int last_no = (int)(pow(CHARACTERS,LENGTH)) - 1;
int i = 0, j = 0;
int a[LENGTH] = {0,};
char p[CHARACTERS] = "po";
for (i = 0; i <= last_no; ++i) {
convert_to_binary(a,i);
for (j = 0; j < LENGTH; ++j)
printf("%c",p[a[j]]);
printf(" ");
memset(a,0,sizeof(a));
}
printf("\b\n");
getchar();
}
#include <string>
#include <iostream>
using namespace std;
void allp(const string& s, int lth, string& out)
{
if (out.size() == lth)
{
cout << out << endl;
return;
}
for (int i = 0; i < s.size(); ++i)
{
out.append(1, s.at(i));
allp(s, lth, out);
out = out.substr(0, out.size() - 1);
}
}
int main()
{
string out;
allp("op", 3, out);
}
Common Question on Combination.
- hprem991 October 28, 2011Write recursive function which will limit the maximum call upto depth 3 and manipulate the string content.