Amazon Interview Question for Testing / Quality Assurances


Country: India
Interview Type: In-Person




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

Here is the python version for it

import itertools
def permutation(word):
    for i in range(len(word)+1):
        perms = list(itertools.permutations(word,i))
        #perms = list(itertools.combinations(word,i))
        for i in perms:
            print ''.join(i)

If combination is needed use the commented out line.

- Naveen April 14, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
1
of 1 vote

We can use a random function on the input string and store the values returned in a hash to avoid any duplicates.

regards
www udzial com
Udzial Means Share

- Gaurav Khurana April 17, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

import java.util.ArrayList;
import java.util.HashSet;
import java.util.Iterator;
import java.util.TreeSet;


public class Comb {
	TreeSet hs = new TreeSet();	
	public TreeSet permutation(String prefix, String str) {
        int n = str.length();
        if (n == 0)
        {
        	hs.add(prefix);
        	//System.out.println(prefix);
        }
        else
        {
        	hs.add(prefix);
            //System.out.println(prefix);
        }
            for(int i = 0;i < n;i++)
                permutation(prefix+str.charAt(i), str.substring(0, i)+str.substring(i+1, n));
            return hs;
        
    }
	void print (TreeSet hs)
	{
		Iterator itr = hs.iterator();
		while(itr.hasNext())
		{
			String str = (String)itr.next();
            System.out.println(str);
		}
	}


	public static void main(String[] args) {
Comb b = new Comb();
String ss = "abc";
TreeSet hss = b.permutation("", ss);
System.out.println("Printing in acsending order....................");

b.print(hss);
int i;
ArrayList al = new ArrayList();
for (i = 1;i<=ss.length() ; i++)	
{
	Iterator itr = hss.iterator();
	while(itr.hasNext())
	{
		String str = (String)itr.next();
		if(str.length()==i)
		{
			al.add(str);
		}
	}	
}
System.out.println("Printing in acsending order and ascending length....................");
for (i=0;i<al.size(); i++)
{
	System.out.println((String)al.get(i));
}
	}

}

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

void permute(char s[],int left,int n)
{
if(left>=n)
{
printf("%s\n",s,left,n);
return;
}
int k=0;
for(k=left;k<n;k++)
{
swap(s,left,k);
permute(s,left+1,n);
swap(s,left,k);
}
}

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

public static void permutation(String str) {
permutation("", str);
}

private static void permutation(String prefix, String str) {
int n = str.length();
if (n == 0) System.out.println(prefix);
else {
for (int i = 0; i < n; i++)
permutation(prefix + str.charAt(i), str.substring(0, i) + str.substring(i+1, n));
}
}

- Satish April 13, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

So basically what its asking is
all the subsets of the given string set.

That would include any anagrams as well as combinations of any number of characters in the string.

List<string> GetAllCombinations ( string s )
{
	List<string> allSubsets;

	if(s.Length == 0 || s == Null)
	{
		allSubsets = new List<string> ();
		alSubsets.add(new string(""));
	}
	else
	{
		// Get first character.
		char first = s[0];

		// Get all subsets of the remaining string.
		allSubsets = GetCombinations( s.Substring(1) );

		List<string> moreSubsets = new List<string> ();

		foreach( string t in allSubsets)
		{
			for(int i = 0; i < t.Length; i++)
			{
				// Insert the first character in every position of t
				t.InsertCharAt( first, i );
				moreSubsets.Add(t);
			}
		}

		// Add all the strings in moreSubsets to the original list.
		allSubsets.AddAll(moreSubsets);
	}

	return allSubsets;
}

- PrateekS. July 22, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

I haven't have the code yet but you can try it out by having a same binary digits set
which means
hello have five letters so the comparative hash will be in binary of five digits so the total combination will be 2^5 =32 ; now HELLO means = all binary on set (11111); you can take this example to build a valid algorithm

- Abhay November 29, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Here you go

void printanagram (char *s){
	int count;
	int len;
	
	len = strlen(s);
	
	//Run loop via all possible combinations 0,2^len
	for(count=0;count <= pow(2,len);count++){
		int i,j;
		
		for(i=1,j=0;i<=count;j++,i<<=1){
			//Check if current count & with iterator is nonzero.
			// or simply check if that bit is set to one 
			if(count&i)
				printf("%c",s[j]);
		}
		printf("\n");
	}
}

- NewGuy November 29, 2015 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

import java.util.HashSet;
import java.util.*;
class Anagram1{

    private static String inputStr;
    private static Set<String> set=new HashSet<>();
    public static void permute(String arg){
        inputStr = new String(arg);
        System.out.println("inputStr = " + inputStr + " len:" + inputStr.length());
        permute("", 0, new boolean[inputStr.length()]);

        for(String x:set){
            System.out.println(x);
        }
    }

    private static void permute(String str, int index, boolean[] used){
        if(index == inputStr.length()){
            set.add(str);
            return;
        }
        else{
            for(int n = 0; n < inputStr.length(); n++){
                if(used[n]){
                     continue;
                }
                used[n] = true;
                set.add(str);
                permute(str + inputStr.charAt(n), index+1, used);
                used[n] = false;
            }
       }
    }

}

- blurred July 21, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

I'm sorry, but how is "hel" an anagram of hello?

- DumbDUm September 08, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public class StringPermutations { public static void main(String args[]) { permutation("hello"); } /* * A method exposed to client to calculate permutation of String in Java. */ public static void permutation(String input){ permutation("", input); } /* * Recursive method which actually prints all permutations * of given String, but since we are passing an empty String * as current permutation to start with, * I have made this method private and didn't exposed it to client. */ private static void permutation(String perm, String word) { if (word.isEmpty()) { System.err.println(perm + word); } else { for (int i = 0; i &lt; word.length(); i++) { permutation(perm + word.charAt(i), word.substring(0, i) + word.substring(i + 1, word.length())); } } } }

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

import java.util.ArrayList;


public class annagrome {





public static void main(String[] args){
String arun="hello";
ArrayList<String> ang=new ArrayList<String>();
char[] ar=arun.toCharArray();
for(int i=0;i<ar.length;i++)
{
String ss=""+ar[i];
System.out.println("===="+ss);
for(int j=0;j<ar.length;j++)
{
if(i!=j)
{
ss=ss+ar[j];
ang.add(ss);
}
}

}

System.out.println(ang);

}

}

- Arun April 27, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
-1
of 1 vote

import java.util.ArrayList;
public class annagrome {
public static void main(String[] args){
String arun="hello";
ArrayList<String> ang=new ArrayList<String>();
char[] ar=arun.toCharArray();
for(int i=0;i<ar.length;i++)
{
String ss=""+ar[i];
System.out.println("===="+ss);
for(int j=0;j<ar.length;j++)
{
if(i!=j)
{
ss=ss+ar[j];
ang.add(ss);
}
}}

System.out.println(ang);

}

}

- Arun April 27, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
-2
of 2 vote

C#

static bool isAnagramm(string pattern, string input)
        {
            var a1 = pattern.ToArray();
            var a2 = input.ToArray();

            Array.Sort(a1);
            Array.Sort(a2);


            return new string(a1) == new string(a2);
        }

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

Why is this down voted ? Is it because it doesn't check the variable Lengths ?

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

whats the logic ?
how it will get all the combination with same sort funtion called everytime ?
why there are two things needed ,, i mean 'pattern' 'input'

- Gaurav Khurana April 17, 2014 | Flag


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