Amazon Interview Question for Software Engineer / Developers






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

probably try to use factorial method to generate all possible strings from a given string. eg, string "abc" can be written as {abc,acb,bac,bca,cab,cba}..So, we got 3! number of words...this technique can be used along with rearranging the string

- T October 12, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public class PermutationOfStrings {

/**
*
* @param arr
*/
static void printArray(char arr[]) {
System.out.print("\n");
for (int i = 0; i < arr.length; i++) {
System.out.print(arr[i]);
}
}

/**
*
* @param i
* @param j
* @param arr
*/
static void permute(int i, int j, char arr[]) {
if (i == j) {
printArray(arr);
} else {
permute(i + 1, j, arr);

for (int z = i; z < j; z++) {
char temp = arr[i];
arr[i] = arr[i + 1];
arr[i + 1] = temp;
permute(i + 1, j, arr);

}

}
}

/**
*
* @param args
*/
public static void main(String[] args) {

permute(0, 3, new char[] { 'a', 'b', 'c', 'd' });
}

}

- Amit October 12, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

String ConcatenateStr(String str) {
return str +str;
}
String reverseString(String str){
   return str.reverse();//reverse the string using any algo.
}

printPermutOfString(String str){
int sze= str/2;
  for(int i=0;i<sze; i++){
    int j=i;
    Sysout(str.subStr(j,sze); //extracts substr from index j to length sze;
}
}
PSV main(String args[]){
  String input = "abc";
  String concStr = ConcatenateStr(input);  //abcabc
  printPermutOfString(concStr);//abc,bca,cab --output
String revStr = reverseStr(concStr);cbacba
printPermutOfString(revStr )//cba,bac,acb --ouput

}

- Neeru October 13, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public class StringCombinations {

	public static void main (String[] args) {
		StringCombinations s = new StringCombinations();
		s.printStringCombinations("abcd".toCharArray(), 0);
	}

	private void printStringCombinations(char[] s, int level) {

		if (level == s.length) {
			System.out.println(s);
			return;
		}

		for (int i = level; i < s.length; i++) {
			swap(s, i, level);
			printStringCombinations(s, level + 1);
			swap(s, i, level);
		}
	}

	private void swap (char[] s, int position1, int position2) {
		char temp = s[position1];
		s[position1] = s[position2];
		s[position2] = temp;
	}
}

- intrepid_interviewee October 14, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

I think so the question is to print all the combinations from a string ..not the permutations..

- musheka November 08, 2009 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

public static void stringCombinations(String prefix, String str){

if(str.length() == 0){
System.out.println(prefix);
return;
}

for(int i = 0 ; i < str.length() ;i++){

stringCombinations(prefix + str.charAt(i), str.substring(0, i) + str.substring(i+1, str.length()));

}
}

- A January 20, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Here I am assuming we can convert string to a array of characters and then proceed.

Length of a substring can be between (1 n), for any given length m, find all the combination of a binary array which contain m 1s and rest are all 0. and then print only those value of original array for which value in binay array is 1.

for example: input: 'abc', for length 1, binary array can be [100] [010] [001] == > print ==>a,b,c
for length 2, binary array [110][101][011] ==> ab, ac, bc

Here is working code

public class StringSubSet {
	public static void PrintCombiOfGivenLength(char[] B, int[] Helper, int start, int remain){
		if(remain == 0) {
			System.out.print("[");
			for (int i=0;i<Helper.length;i++){
				if(Helper[i] == 1)System.out.print(B[i]+",");
			}
			System.out.println("]");
			return;
		}
		else{
			for(int i=start;i<B.length;i++){
				Helper[i] = 1;
				PrintCombiOfGivenLength(B,Helper,i+1,remain-1);
				Helper[i] = 0;
			}
		}
	}
	public static void main(String[] args) {
		char[] B = {'a','b','c'};
		int[] Helper = new int[B.length];
		for(int i=1; i<=B.length; i++) {
			PrintCombiOfGivenLength(B,Helper,0,i);
		}
	}
}

- satyans24 November 21, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

We can do the permutation of the Characters.
here is the Java code:

import java.io.*;

public class StringPermutaion {
static StringBuffer result;
static int len=0;
public static void main(String[] args) throws IOException{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String input = br.readLine();
len = input.length();
result = new StringBuffer(input);
permute(0);
}
static void permute(int i){
if(i==len)
System.out.println(result);
else{
for(int j=i;j<len;j++)
{
swap(i,j);
permute(i+1);
swap(i,j);
}
}
}
static void swap(int i, int j){
char temp = result.charAt(i);
result.setCharAt(i, result.charAt(j));
result.setCharAt(j, temp);
}
}

- Ravi Ranjan March 08, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

how to print like below pattern through java language
B C
A

- Anonymous July 23, 2017 | 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