Ctrl K

A Bit in the Mirror-Grown String

Medium
A string of zeros and ones grows step by step. After the first step it is "0". On every following step, the current string t gets a "1" appended on the right, followed by a copy of t with every bit flipped (0 ↔ 1) and the characters in reverse order. This produces "0", "011", "0111001", "011100110110001" and so on: after step i the string has length 2^i − 1. Given steps and pos, return the character ("0" or "1") at position pos (1-based) of the string obtained after steps steps. The string grows exponentially, so try to answer without building it.
Example 1
steps=3, pos=5 → '0'

After three steps the string is 0111001; its fifth character is 0.

Example 2
steps=4, pos=8 → '1'

The string 011100110110001 has length 15; the eighth character is the middle 1 added on the last step.

Example 3
steps=1, pos=1 → '0'
Constraints
  • 1 ≤ steps ≤ 20
  • 1 ≤ pos ≤ 2^steps − 1
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsSeeded Playoff Bracket MediumMultiplying Digit Strings MediumEvery Bracketing Result MediumNumbers That Survive a Flip MediumExpand Repeat Brackets Medium
loading Python…
Input
steps=3, pos=5
Expected
'0'

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