Amazon Interview Question for Software Engineer / Developers


Country: India




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

In the simple hash map solution the complexity is o(n) + o(n). To prevent the second pass

We can do this:

Store the pointer to the hash map elements in another link list. This link list maintains the order of characters. That is it has the pointers of the hash map in the same order as the character appears in the sequence.

When you are done traversing the sequence. Look at the link list and check the value of hash map address with count = 1. Keep on counting this until u get nth element.

- Akshat January 09, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Yes. I did something similar. created a structure called node, that has value, and count. and built a link list (to preserve the order). This is done with only a single pass of the input array.

- P January 09, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

We can map the streams of characters into a hashmap, and scan the streams again to find n th non repeating number.

- zhouzhou January 09, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

use a bit map, instead of hashtable.

- BVarghese January 09, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

bit map is not gonna work, as one bit can only represent two states, but here there are three states, not exist, once, more than once

- Anonymous January 11, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Keep the count of characters in a Hash Map of Size 26( A to Z ). Once you are done with updating the hash map. Scan again to find out the nth number which has a count 1.

- Akshat January 09, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

This won't preserve the order.

- P January 09, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

C++ code,

// Find the nth non-repeating number given a streams of characters in the range of A-Z
    char FindnthNonRepeatNum(char *str, int k)
    {
        if (!str || 0 == k) return '\0';

        char *p = str;
        std::map<char, int> numMap;
        std::map<char, int>::iterator it;

        std::vector<char> numV; // use the vector to save the order of char occurance
        std::vector<char>::iterator itc;

        while (*p != '\0')
        {
            it = numMap.find(*p);

            if (numMap.end() != it)     // current char has been added to map;
            {
                (*it).second += 1;      // count > 1 mean, the number repeats.
            }
            else
            {
                numMap[*p] = 1;
                numV.push_back(*p);     // not found in map, so add it to vecotr.
            }

            ++p;
        }

        int kth = 0;
        for (itc = numV.begin(); itc != numV.end(); ++itc)
        {
            if (kth != k && 1 == numMap[*itc])  // the char only occur once
            {
                kth++;
                if (kth == k) break;
            }
        }

        if (k != kth)
            return '\0';
        else
            return (*itc);
    }

- Yaya January 09, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

We can maintain an array of 26 size to store the status of each alphabet as it will belong to only A---Z.

getNthRepeatCharacter (n){

int[] repeatStatus = new int[26];

while((s=stream.read())!=null){

if(repeatStatus[mapCharToPosition(s)] !=0){
  repeatStatus[mapCharToPosition(s)] = -1;
}else{
 repeatStatus[mapCharToPosition(s)]=stream.position;
}

}

sort(repeatStatus) // place -1 at end

return repeatStatus[n];

}

Time taken to iterate through whole stream = length of stream;(let say m)
At end time taken to sort 26 element array can be done in n(logn) time .

Total complexity = m+26log26 = O(m).

- Sidhavratha January 12, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

class Node{
  Character c;
   int count;

  public Node(Character c, int count) {
    this.c = c;
    this.count = count;
  }

  public Character getC() {
    return c;
  }

  public void setC(Character c) {
    this.c = c;
  }

  public int getCount() {
    return count;
  }

  public void setCount(int count) {
    this.count = count;
  }
}


/**
 * @author pmundra
 */
public class nonRepeatingNthCharacterInStream {
  public static void main(String[] args) {
    String s="aaabcdeeffghh";
    Map<Character,Node> m= new HashMap();
    List<Node> l=new LinkedList();
    int i=0;
    while(i<s.length()){
     Character c= s.charAt(i++);
     Node n= m.get(c);
      if(n==null){
        Node newNode=new Node(c,1);
        m.put(c,newNode);
        l.add(newNode);
      }
      else{
       n.setCount( n.count+=1);
      }
    }
    i=0;
    for(Node n:l){
      if(n.count==1){
        System.out.println(n.c);
        i++;
      }
      if(i==5){
        System.out.println(">>>>>>>>> "+n.c);
      }
    }
  }
}

- priyank.mundra January 14, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

#include <iostream>

using namespace std;

class NthNonRepeat {
public:
static char find(string str, int n);
};

char NthNonRepeat::find(string str, int n) {
int counts[26];
int cur = 0;

for (int i = 0; i < 26; ++i) {
counts[i] = 0;
}
for (int i = 0; i < str.length(); ++i) {
if (counts[str[i]-'a'] == 0) {
cur++;
}
counts[str[i]-'a']++;
if (cur == n)
return str[i];
}
return 0;
}

int main() {
string str = "kkkaaabbbcjjjmmmtteepp";
int n = 5;
cout << NthNonRepeat::find(str, n) << endl;
}

- vn January 16, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

use LinkedHashMap

- theval January 21, 2012 | 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