Yahoo Interview Question Software Engineer / Developers

• 0

Let f(k) = y where k is the y-th number in the increasing sequence of non-negative integers with the
same number of ones in its binary representation as y, e.g. f(0) = 1, f(1) = 1, f(2) = 2, f(3) = 1,
f(4) = 3, f(5) = 2, f(6) = 3 and so on. Given k >= 0, compute f(k).

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

Use recursion.
Base case- f(k): if k has binary digits with all 1s aligned to the right,(and all 0s to the left) return 1;
Recursion- f(k) : return 1 + f(next_lesser_num(k))

next_lesser_num(k) = start from LSD. keep going to the left until you encounter a 0. Now, keep going to the left until to encounter a 1. Flip this 1 to 0 and the zero to the immediate right to 1.

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

Name:

Writing Code? Surround your code with {{{ and }}} to preserve whitespace.

Books

is a comprehensive book walking you through every aspect of getting a job at a top tech company, while focuses on software engineering interviews.

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.