Vel
BAN USER- 0of 0 votes
AnswersA bank has a collection of n bank cards that they’ve confiscated, suspecting them of being used in a fraud. Each bank card corresponds to a unique account in the bank. Each account can have many cards corresponding to it, and we’ll say that two bank cards are equivalent if they correspond to the same account. The only way to say 2 cards are equivalent is by using a high-tech “equivalence-tester” that takes in 2 cards, and after performing some computations, determines whether they are equivalent.
- Vel
Their question is the following: among the collection of n cards, is there a set of more than n/2 of them that are all equivalent to one another? Assume that the only feasible operations you can do with the cards are to pick two of them and plug them in to the equivalence tester. Answer in O(n)| Report Duplicate | Flag | PURGE
Microsoft Software Engineer / Developer Algorithm
weicool's impl is is more like a lookup array than a hash table. It fails in the case a node gets added/deleted in the middle of the array. this would require reindexing all the elements following that node in the list. a better solution would be a DBMS approach, which requires some addl info abt the data being stored in each node of the linked list. if the node contained something similar to a primary key, then we can hash on that field to get the key and store the address of the node as the value. if thers is no such unique field, then we can resort to chaining.
- Vel March 28, 2008Can be solved in O(logn) space and O(n) time
1. Do DFS
2. During DFS, if you find one of the nodes, store the stack contents(path from the root). Repeat for both nodes. This requires 2logn space
3. Now compare both these paths from root.. the last common node in the path is the first common ancestor. This takes 'logn' time in avg case and 'n' in worst case.
Anonymous is right !
- Vel March 29, 2008but this requires the input to be sorted, in case it is not. Overall soln O(nlgn)