Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

amazon interview prob

Tags:

algorithm

There is a big file of words which is dynamically changing. We are continuously adding some words into it. How would you keep track of top 10 trending words at each moment?

I found this question in a blog but I couldn't understand the answer. The answer is: hash table + min-heap

I understand why hashtable but not min-heap part, can someone help me?

like image 224
rplusg Avatar asked Aug 26 '12 19:08

rplusg


People also ask

What is the success rate of Amazon interview?

A successful outcome from the Debrief part of the Amazon interview process leads to an offer in 9 out of 10 cases. However, as you can anticipate, sometimes things don't follow this script.

Are Amazon interview questions repeated?

Can you expect a repeat Amazon interview question? No. Each interviewer should be assigned 2 completely unique Leadership Principles to test you.

Is Amazon coding interview hard?

Amazon coding interviews are really challenging. The questions are difficult, specific to Amazon, and cover a wide range of topics. The good news is that the right preparation can make a big difference.

Why is Amazon interview so hard?

Amazon's Behavioral Interview Is Tricky And Challenging Engineers applying to positions across the board go through a mandatory behavioral interview where they're assessed on a bunch of behavioral aspects. The questions are usually around Amazon's 14 leadership principles that the company values deeply.


1 Answers

If it's top 10 trending words then you should use a max-heap along with a hash-table.

When a new word is added to the file then:

  • Create a new element x with x.key=word and x.count=1.
  • Add x to the hash-table. O(1).
  • Add x to the max-heap. O(lgn).

When an existing word is added to the file then:

  • Find x in the hash-table. O(1).
  • Update x.count to x.count++.

When there is a need to retrieve the top 10 trending words then:

  • Extract 10 times from the max-heap. 10*O(lgn)=O(10*lgn)=O(lgn).

As you can see, all the needed operations are done in at most O(lgn).

like image 87
Avi Cohen Avatar answered Oct 30 '22 21:10

Avi Cohen