Ctrl K

Code by Its Position

Medium
List every string made of the characters 0 and 1 in this order: first by length (shortest first), and strings of the same length in increasing order as binary numbers. The empty string comes first: "", "0", "1", "00", "01", "10", "11", "000", ... Positions start at zero. Given num, return the string at position num.
Example 1
num=5 → '10'

The list starts "" (0), "0" (1), "1" (2), "00" (3), "01" (4), "10" (5).

Example 2
num=0 → ''

The very first code is the empty one.

Example 3
num=13 → '110'

There are 7 codes of length at most 2 (numbers 0–6), then eight codes of length 3: "000" is number 7, and "110" is number 13.

Constraints
  • 0 ≤ num < 2⁶²
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsNumbers Made of Fours and Sevens MediumSum of Binary Strings EasyNumber in Roman Numerals MediumDivision Without Dividing MediumMultiplying Digit Strings Medium
loading Python…
Input
num=5
Expected
'10'

Run checks the first 3 tests in your browser. Submit checks all 113 tests on the server, hidden ones included.