Interview Question


Country: United States




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

Interesting problem. The first idea that comes to mind is a greedy algorithm, where I split my current array value v[i] only if v[i] > sum(v'[j]) where sum(v[j]*2^j) = v[i]*2^i for 0 <= j <= n

So the algorithm will be:

min_sum = 0
for i in 1 to n:
  if (v[i] > sum(1's in binary representation of v[i]) then:
     min_sum = min_sum + sum(1's in binary representation of v[i])
  else: min_sum = min_sum + v[i]
return min_sum

As you mention that you can always add more empty spots to the input array v if need be, and as we're guaranteed that v[i] will only affect any elements coming AFTER it, we will always be able to get to the last index of v.

If we assume just 3-bit numbers for simplicity, a number n > sum(1's in n's binary representation) just after n = 2, so almost always, we will see a replacement. Since they are 3-bit numbers, each number can at-max affect 2 bits after itself...with a bounded start, this means that the algorithm terminates in worst-case (n + 3) iterations.

- Killedsteel February 20, 2017 | 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