Count Smaller Numbers with Only Zero and One in Base Four
Company: Hudson
Role: Data Scientist
Category: Coding & Algorithms
Difficulty: medium
Interview Round: Online Assessment
Overview: Count positive integers below a strict upper bound whose base-4 representation contains only zeros and ones, without scanning every smaller integer.
Read the full Hudson Data Scientist interview experience this question came from
Constraints
- 1 <= n <= 1000000000000000; n and the returned count are exact ordinary decimal integers.
- Count only positive integers strictly less than n whose usual base-4 representation contains only digits 0 and 1.
- Zero is excluded, and each value is counted once; leading zeros do not create additional values.
- Do not enumerate every positive integer below n.
Examples
Input: (17,)
Expected Output: 4
Explanation: Published sample 1: 1,4,5,16 are the four qualifying positive values.
Input: (5,)
Expected Output: 2
Explanation: Published sample 2: only 1 and 4 are strictly smaller.