Amazon Interview Question for Software Engineer / Developers






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

Find positions of the chars F and O in string
F -> 1,7
O -> 2,3,8,9
Building this takes O(n) time.

Now maintain 2 pointers over the 2 list, similar to merge process in merge sort. and at any point if O[i]=F[i]+1, increment your counter.

Another O(n) time. So total is O(n) with O(n) space to maintain the lists.

- Anonymous November 20, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

if the string length is very big what will you do for that?

- Anonymous November 20, 2009 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

If the string is long, here is what you do.
haystack -> original string . length n
needle -> the substring you are trying to match. lenght k (<=n)

step 1)
for each char in needle
   put char in hash
O(k) 

step 2)
for each char in haystack
   if char in hash
     if list doesnt exist for char, create list for that char
     append position to its list.
O(n)
note that all positions in the lists will be sorted


step 3) merging of the k lists.  
This is only pseudo code, havent checked for boundary conditions, errors etc

int ptrs[k] -> maintain an index for each list, initially all 0.
count=0;
lists_not_empty=true;
while(lists_not_empty)
{
 for i=2 to k
 { 
  if (list[i].end)
    lists_not_empty=false;
  /* get the ptrs[i]'th elem in the ith list   */
   while( (lists[i])[ptrs[i]] < (lists[i-1])[ptrs[i-1]] )
           ptrs[i]++;  
   if(lists[i])[ptrs[i]] + 1 != (lists[i-1])[ptrs[i-1]])
           ptrs[1]++; break; 
  } //end of for

  if( (lists[k])ptrs[k] + k ==  (lists[1])ptrs[1]
    count ++;
}

- blah November 20, 2009 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

step 3 is O(n). So total is O(n)

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

Using substr and strindex functions, it can be done in O(n squared) time.

Using a hash table with keys as all two element sub-strings and values as number of times the sub-string was seen earlier, we can do it in O(n) time.

- hash_table November 20, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

For hash working based on the indexing, but in your case the key of the hash is string. i don't think so it will work.

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

key-count
key=2 length string
FO-2
OO-2
OP-1
PA-1
AR-1
RF-1

O(n) time, O(n) space

- Use Hashtable... November 26, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Use hashtable.

- Anon November 26, 2009 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

What hashtable? You need to define it.
For ASCII chars, use a 256 x 256 integer array(26 x 26, if A-Z and a-z only).
So if we see AA, increase C[0][0] by 1. For FO, increase C[4][14] by one.

We need only scan the string n-1 time and then report those larger than 2 in the array.

O(n) space does not make sense.

- kulang January 02, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Use hash table
Insert FO in the table
Than insert OO in the table
than OP
PA
AR
Find FO dont insert, print found "FO"
find OO dont insert, print found "OO"

O(n)

- Siddharth December 01, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

what if some continuous repeated chars are specified , for ex : FOOOAHHD. Here O occurs three times, so is it OO repeating two times?

- Anonymous December 05, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

You should be looking at the Knuth-Morris-Pratt Algorithm. It would be able to find matches in time O(m+n) where m and n are the length of pattern and the string correspondingly.

- snowbeard December 07, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Could you help clarify the Question, is that
i) any 2Char sized string, e.g. both FO & OO in FOOPARFOO
ii) a fixed one - (FO only or OO only)

If (ii) the Knuth-Morris-Pratt Algorithm should be good, if (i) I can think
of a O(n^2) solution too.

- bitmapper December 09, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Generate a suffix tree out of the given word. Now look for the "branch" node with atleast 2 children and min depth of 2.

- geek January 03, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

I agree with Geek. I was going to but he told it already.

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

The given input string is FOOPARFOO.... We use hash table to solve the problem ..
First we hash 'FO' then 'OO' then 'OP' then 'PA' and so on. At some point when we try to hash ' FO' we see that the entry for 'FO' already exists at that point we conclude that a duplicate has been found
Time complexity is O(n) and space complexity is O(n)

- abhimanipal April 08, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Will this work for FOOOPARFOO?

- Tom June 06, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

While hashing all two char words also keep track of index pair in case of collision first check if pair1.second == pair1.first ignore it.

- M August 09, 2010 | 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