Amazon.com
3.4 of 5 2,792 reviews
www.amazon.com Seattle, WA 5000+ Employees

Amazon.com Software Design Engineer Interview Question

I interviewed in Seattle, WA and was asked:
"Given a string find the first non-repeated character."
Tags: data structures, programming, string manipulation
Add Tags [?]
Answer Flag Question

Part of a Software Design Engineer Interview Review - one of 4,127 Amazon.com Interview Reviews

Answers & Comments

0
of 0
votes
Hint: use a hash table
- Interview Candidate on Mar 19, 2009 Flag Response
0
of 0
votes
public static char getFirstNonRepeatedChar(String s) {
        List<Character> charList = null;
        char nonRepeatedChar ='?';
        if (s != null) {
            s = s.trim();
            charList = new ArrayList<Character>();
            for (int i=0; i<s.length(); i++) {
                Character c = s.charAt(i);
                if (!charList.contains(c)) {
                    charList.add(c);
                } else {
                    charList.remove(c);
                }
            }
        }
        if (charList != null && !charList.isEmpty()) {
            nonRepeatedChar = charList.get(0);
        }
        return nonRepeatedChar;
    }
- Rajiv on Aug 23, 2010 Flag Response
1
of 1
vote
@Rajiv : Your solution is completely wrong. It will fail for input of "aaa"
Reason: on first check, you insert "a". On next check you remove it. On next check you again insert it and return that as your answer, even though it was repeated thrice.
- Kumar Manish on Feb 13, 2011 Flag Response
0
of 0
votes
hash of non repeating characters tied to a double linked list, remove any repeating character from the hash and the list. at the end the head of the list is the answer. you can use the same hash to keep the counters.
- dantepy on Apr 30, 2011 Flag Response
0
of 0
votes
public static String findFirstNonRepeatedCharacter(String S) {

        int[] T = new int[256];
        for (int i = 0; i < S.length(); i++) {
            char next = S.charAt(i);
            T[next] = T[next] + 1;
        }

        for (int i = 0; i < S.length(); i++) {
            if(T[S.charAt(i)] == 1)
                return String.valueOf(S.charAt(i));
        }

        return null;
    }
- AB on Jun 14, 2012 Flag Response
0
of 0
votes
Three ways to find first non repeating character in a string c#

find the code below -

public char firstNonRepetitive(string inputString)
{
    int nextOccurrence = 0;
    char firstDistinctChar = ' ';
    for (int i = 0; i < inputString.Length; i++)
    {
        nextOccurrence = 0;
        for (int j = (i + 1); j < inputString.Length; j++)
        {
            if (inputString[i] == inputString[j])
                nextOccurrence++;
        }
        if (nextOccurrence == 0)
        {
            firstDistinctChar = inputString[i];
            break;
        }
    }
    return firstDistinctChar;

}
Check out amazing two more way -

program to find first non repeating character in a string c# - three ways

http://www.dotnetbull.com/2013/08/find-first-non-repeating-character-string.html
- Vivek Gupta on Aug 15, 2013 Flag Response
0
of 0
votes
<a href='http://www.dotnetbull.com/2013/08/find-first-non-repeating-character-string.html' >program to find first non repeating character in a string c# - three ways<a>
- Deepti Thakur on Aug 15, 2013 Flag Response
0
of 0
votes
My python implementation:

def firstNonRepeatingCharacter(inputString):
    hashmap = {}
    for x in inputString:
        if (x in hashmap):
            hashmap[x] = hashmap[x] + 1
        else:
            hashmap[x] = 1
    for x in inputString:
        if (hashmap[x] > 1):
            continue
        else:
            return x
    return "No nonrepeating character found"
- Sina Yeganeh on Sep 5, 2013 Flag Response

To comment on this question, Sign In with Facebook or Sign Up


Tags are like keywords that help categorize interview questions that have something in common.

Glassdoor is your free inside look at Amazon.com interview questions and advice. All interview reviews posted anonymously by Amazon.com employees and interview candidates.