Amazon Interview Question for Software Engineer / Developers


Country: United States
Interview Type: Phone Interview




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

There are several ways to do this, using Vectors, HashMaps, etc.
You can do it creating another String and checking if the next character is at the String.
The first occurrence that returns true, that is your first repeated character.

- Leandro Assad Martins October 22, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public static void main (String args[])
	{
		
		char returnedValue = repeatedCharacter("stqwbbertygfyaa");
		if (returnedValue == '!')
		{
			System.out.println("String does not contain any duplicate values");
		}
		else
		{
			//we found the real character
			System.out.println(returnedValue);
		}
		
		
	}
	
	public static Character repeatedCharacter(String myString)
	{
		char prev = myString.charAt(0);
		for(int i = 1 ; i < myString.length() ; i++)
		{
			if(prev == myString.charAt(i))
			{
				return myString.charAt(i);
			}
			prev = myString.charAt(i);
		}
		return '!';
	}

- Anonymous October 22, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

This code only works if the repeating character appears consecutively like "abccd".

However, your code would not work with the following string "abcdec" where 'c' is the first repeating character.

- Anonymous October 22, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

The 'easiest' approach would be any type of associative array with the count as the value. This would be O(n) assuming your associative array is long enough to support the keys required for the string. Some data structures give you O(1) for put / get with 'high probability.'

By "first repeating..." I'm assuming you mean the first time a character repeats in the array.

So, if you have {a,b,d,b,e,e,a}

Your answer would be b, which repeats at index 3, not a which appears first at index 0 and again at 6.

In your associative array, you'll have

[ a=6, b=3,d=-1,e=5 ]


If you have a minimal array, you could traverse over that array, but you'll probably not have one. In most cases, you should traverse over the string again and the character with the lowest number for the value would be the answer. In this example, I used -1 to note that the character did not repeat.

- nothing special here October 22, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

In Java, use a HashSet. Constant insert and lookup (amortized), and unlike an array it's sparse which means the size doesn't depend on the theoretical size of the charset (which for unicode is a lot).

public Character find(String s) {
        if (null == s) throw new IllegalArgumentException("Null input");
        
        HashSet<Character> seenCharacters = new HashSet<>();
        
        for (char c:s.toCharArray()) {
            if (seenCharacters.contains(c)) return c;
            seenCharacters.add(c);
        }
        
        return null;
    }

- Java7 October 22, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

fastest way to do is
create a boolean array of size 26. for each letter you find
public char findRepeatedChar(String str){
boolean flag[] = new flag[26];
for (int i = 0; str.length() ; i++){
char c = str.charAt(i);
int index =c - 'a';
if (flag[index]) {
return c;
}else{
flag[index] = true;
}

}

}
}

- Anonymous October 22, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

we can also try bitwise operator on integer (integer == 32 bits)
int test;
if( test & 1 >> (array[i]-'a'))
print duplicate
else
test = test + 1 >> (array[i]-'a')

- RAM October 26, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

sorry for the mistake it right shift

- RAM October 26, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Fastest way to do it is using a hash. At each char check if it is in the hash and if it's not put in there. If it is you've found your first repeating char and you're done

- pittbull October 22, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

import java.util.HashSet;
import java.util.Set;

/**
 * 
 *This code assumes the small and capital letters as different elements
 *Also it prints whitespace if that is repeating. 
 *Un-comment the commented part of the code to remove the above assumption.
 *
 */
public class StringRepeatingCharTest {

	public static void main(String[] args) {
		String str = "thunderbird";
		//str = str.toLowerCase();
		//str = str.replaceAll(" ", "");
		char[] strChars = str.toCharArray();
		Set<Character> set = new HashSet<Character>();
		boolean flag = true;
		for(Character c : strChars){
			flag = set.add(c);
			if(!flag){
				System.out.println("First repeating character is : " + c);
				break;
			}
		}
	}

}

- Anonymous October 22, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

import java.util.HashSet;
import java.util.Set;

/**
 * 
 *This code assumes the small and capital letters as different elements
 *Also it prints whitespace if that is repeating. 
 *Un-comment the commented part of the code to remove the above assumption.
 *
 */
public class StringRepeatingCharTest {

	public static void main(String[] args) {
		String str = "I am working in Amazon";
		//str = str.toLowerCase();
		//str = str.replaceAll(" ", "");
		Set<Character> set = new HashSet<Character>();
		boolean flag = true;
		for(int i=0;i<str.length();i++){
			flag = set.add(str.charAt(i));
			if(!flag){
				System.out.println("First repeating character is : " + str.charAt(i));
				break;
			}
		}
		
	}

}

- Anonymous October 22, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

This can be solved in O(n) time. If we only consider ASCII characters array of 256 numbers is required.

Whenever a character is found its corresponding ASCII value index in array is changed to 1 to remember that this character is already seen. Whenever a new character comes in it is checked with its corresponding ASCII value index in array and if it is already seen then loop breaks.

Remeber this code treats upper and lower case letters differently.

char str[] = "tarun gupta";
	int l = strlen(str);
	int flag[256]={0};

	for( i=0 ; i<l ; i++){
		if ( flag[str[i]]!=0 ){
			printf("The first repeating character is %c", str[i]);
			break;
		}
		else{
			flag[str[i]] = 1;
		}	
	}

- tarun.gupta.pec October 22, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

private void repeatingCharater(String s) {
		Set<Character> set = new HashSet<Character>();
		for (int i = 0; i < s.length(); i++) {
			if (set.contains(s.charAt(i))) {
				System.out.println(s.charAt(i));
				break;
			} else {
				set.add(s.charAt(i));
			}
		}

}

- Anonymous October 22, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public class RepeatChar {

static String s = "abcdefa";
static int a[] = new int[65535];

static void findChar(){

for(int i=0; i < s.length(); i++){
if(++a[s.charAt(i)] == 2){
System.out.println(s.charAt(i));
System.exit(0);
}

}
System.out.println("No repetitions");
}

public static void main(String args[]){
if(s.length()==0)
System.out.println("Empty String");
else
findChar();
}

}

- PTR October 24, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

if(!*s)
  return (char)0;
for(i=0;i<n;i++)
{
  if(s[i] == s[i+1])
      return s[i];
}
return (char)0;

- Anonymous October 24, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Saving it in a temporary DS(in this case, hashmap) and comparing against it for every new char.

private static String findFirstRepeatingChar(String str){
		
		Map<String, Integer> hash= new HashMap<String, Integer>();
		
		for (int i = 0; i < str.length(); i++) {
			
			String temp = str.substring(i, i+1);
			if(hash.containsKey(temp))	
				return temp;
			hash.put(temp, 1);
		}
		return "NOT FOUND";
	}

- UB October 25, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

static void firstRepChar(String s){
		char [] ch=s.toCharArray();
		HashMap<Character, Integer> cMap=new HashMap<Character, Integer>();
		int value=0;
		for(char c:ch){
			value=0;
			if(cMap.containsKey(c)){
				value=cMap.get(c);
			}
			value++;
			cMap.put(c, value);
		}
		
		for(char c:ch){
			if(cMap.get(c)>1){
				System.out.print(c);
				return;
			}
		}
	}

- shashi_kr October 27, 2013 | 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