Binary Gap Exercise in Codility
Find a specific binary sequence.
We'll cover the following...
Suppose a positive integer N
is given. Determine the binary representation of N
, and find the longest subsequence of the form 10*1
in this representation, where 0*
stands for any number of zeros in the sequence. Examples: 11
, 101
, 1001
, 10001
etc. Return the number of zeros in the longest sequence you found. If you didn’t find such a sequence, return zero.
You can read the original task description on Codility.
Solution:
Whenever you deal with a riddle, bear in mind, it doesn’t matter what techniques you use as long as your solution is correct. Don’t try to impress your interviewers with fancy techniques, don’t even think about announcing that you are going to use “functional programming” or “recursion” or anything else. Just get the job done.
Do explain your thought process! If you are on the right track, your interviewers will appreciate relating to how you think. If you are on the wrong track, your interviewers will often help you ...