Interview Question


Country: United States




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

This can be done in expected O(1) time if you unleash the power of randomization.

Pick two distinct indices at random and get the values there. If they are the same, you've found the identical element. If not, repeat this process. Each time you try this, the chance of success is roughly 1/4 (the first element is one of the identical elements with 1/2 probability, and the second element is one of the identical elements with (n/2-1)/n probability, which is almost 1/2 for all but the smallest of inputs). Therefore, the expected number of attempts needed is ~4, which is O(1) attempts. Each attempt just requires looking at 2 random elements, which takes O(1), so this entire algorithm is O(1) in the expectation.

Without randomization, it's not possible to solve this in less than O(N) because an adversary that knows your algorithm would be able to predict all the locations you will read in advance and place only distinct elements in them, ensuring you never see the identical element until you've first read up to N/2-1 locations, which takes O(N) time.

- eugene.yarovoi September 26, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Righto!

- Anonymous September 26, 2012 | Flag
Comment hidden because of low score. Click to expand.
1
of 3 vote

Use hash map. In worst case, you will get answer after visiting first (N/2 + 2) numbers.

- sahaj September 26, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Are you posting questions off the web?

- Anonymous September 25, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

nope...althgh if u hv a link for the solution of this prblm, plz do share...:)

- anandaroop.acharia September 26, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Then please mention the company and state whether it was phone or in-person interview.

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

The point is that the other half is distinct !

So

2 2 1 7 8 9 2 2

So all we need to do is just start reading the list and see if at least one element is repeated.
1. Put each element to a hash and get the count of each element.
2. If even one element got repeated once, we got our odd one out.
3. Worst case run time for this is n/2 (But still O(n) :( )
4. Once we got the odd one out, every other element is distinct.

- janithkv September 25, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
-1
of 1 vote

Do we need something like "boolean isElementUnique(int x)"?

If you want to find identical that can be done in O(n)

- loveCoding September 25, 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