Using a custom Trie like tree (not really a trie, since a single node can hold multiple words - this is because of the sorting)As @ChrisK pointed out, sorting the input and the dictionary is the way to go:My solution is basically @ChrisK solution that actually compiles :)

We maintain 2 hashes:* First hash keep track of voters - this is to check for fraud* Second hash: candidate->voters-count - we keep track of the number of votes for each candidate

In addition we use a class that keep maps between: votes=>hash of candidatesEvery time we add a vote, we go and update this class

The below solution is a bit complicated because it assumes that the number of candidates can be the same as the number of voters...

If we could assume that the number of candidates is small we could simply sort the candidates array at the end (using priority_queue where the "voters-count" is the priority) and just pop the first top 5 from the queue:

Complex code, assuming number of candidates can be the same as the number of voters:

@ChrisK my assumptions are similar to yours, except that I only consider one digit as prefix and suffix. The OP should have given an example for more clarifications. e.g. in your example "192" "1" is the prefix, "2" is the suffix.

@xyz, he might be a Facebook employee? (maybe from HR...). Just a thought :)

Now, about the question: - Create a class named Interval- Since the lists are sorted, we can start the beginning of each list. ** If we find an intersection, we add the result vector the intersection and advance both lists** If no intersection is found, we advance the iterator of the *lower* interval (i.e. the one with the lowest "start" point)The worst case complexity is O(N+M) (where N and M is the number of elements in the arrays)

use min-heap to store all the intervals from both ranges N(logN) + M(logM)Next we loop over all elements in the queue.We sort the intervals by the starting position and the range size (end-start).The loop is Nlog(N)Here is the "OR" function:

We use BFS to traverse the tree while keeping track on each child's parentThe same can be coded using recursion. As the question stated: this method fixes only one broken node (it can be adjusted to continue and scan for more double links):

@Alex I took your code and improved it a bit. I added a "m_totalChars" counter to Hash class. There is no need to "operator==" - you simply check if "m_totalChars" is equal to 0

So the logic for "ContainsAnagram" is now like this:

* Construct a Hash class for the substring
* Loop over the second string, for each char call sub_s_hash.RemoveChar (which reduces the total counter by 1)
* We return sub_s_hash.isEmpty() if its true this means its an anagram

I recursively create subgroups of the initial int array and sum their values.* If the value is greater than the target value - we keep on recursing* If the value is less than the target value, we stop the current recursion branch* In order to avoid duplicates, we keep a set of all the visited subgroups (we do this by creating a unique string key out of the current int array and keep track of it in a hash table)

* This code can be coded without recursion (use queue instead)* One thing that can be done better here: instead of using a string, use some kind of function to generate unique ID per int array

@RajK you are partially correct, the complexity is the same (O((N+M)/2) you only save space. In your code you still need to run over the array so its *not* O(1), since you need to run half way its O((M+N)/2) where M and N are the array length

The idea here:* Each transaction is identified by a string "Payer->Payee"* Before we insert the this transaction to our ledger ("G" in the code) with the given amount, we check if we have a reversed transaction "Payee->Payer" ** If we do have such a transaction, we subtract the amount. *** If the updated amount is negative, we remove the reversed transaction ("Payee->Payer") and insert "Payer->Payee" with the diff amount. *** If the updated amount is zero - remove the reversed transaction and move on to the next transaction *** Else (the reversed transaction is bigger than the current transaction) so just update the reverse transaction amount and continue

Using hash, no recursive functions:* Store all the movies using std::map using int (movie-start-time) as the key and the value is std::unordered_set (hash-table) containing the movies that start at that time

NOTE: I used std::map because the underlying implementation is red-black tree, i.e. the items will be sorted based on the key (i.e. the start-time)

We then iterate over the map and print the possibilities (eliminating duplication as much as we can, we dont want to watch the same movie twice...)

@ChrisK i think that we only disagree on the initial matrix traverse time. You ignore it, while I take it into consideration. If you ignore this step, then yes, this is a traveling salesman problem. I think that we can't ignore the initial matrix traverse time (and this is what my code does). This debate can be solved only by asking the interviewer these kind of questions...

FYI: I also notice that other people assumed the same assumption as I did

@ChrisK Once you have visited each cell once, you have all the cakes AND you also have the coordinates of the "other person" so you can simply use backtracking to its location ( I have implemented this using the "_origin" member in my code)

@ChrisK not really a travelling salesman: all the steps have the same weight (one step for each direction), you don't know the locations of the cheeses, so you must visit each cell once. Notice that in my code I am using this condition:

* Build a graph. Each cell is connected to its adjacent cells marked with 0 or 2 (we can't move to 1s).
* Use BFS to traverse the *entire* graph (we don't know where the cheeses are located nor how many we have, so we need to check them all).
* When we hit the other person cell we keep track for how many steps we needed to get from him to the end of the maze.

What's Going On

Books

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.

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.

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.