Find the n-th binary string ordered by length, then lexicographically
Company: Quora
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: easy
Interview Round: Technical Screen
List every string made of the characters `0` and `1`, shortest first, with strings of the same length in lexicographic order: the empty string, `0`, `1`, `00`, `01`, `10`, `11`, `000`, and so on. Return the n-th string in this list.
### Function Signature
```python
def nth_binary_string(n: int) -> str:
```
### Rules
- Positions are 1-indexed: `n = 1` is the empty string `""`, `n = 2` is `"0"`, and `n = 3` is `"1"`.
- Strings of equal length are ordered lexicographically, with `"0"` before `"1"`.
### Constraints
- `1 <= n <= 10^15`
- `n` can exceed the 32-bit integer range; use 64-bit integers in languages with fixed-width types.
### Examples
**Example 1**
```text
Input: n = 1
Output: ""
```
The list starts with the empty string.
**Example 2**
```text
Input: n = 6
Output: "10"
```
The list begins `""`, `"0"`, `"1"`, `"00"`, `"01"`, `"10"`, so the sixth string is `"10"`.
**Example 3**
```text
Input: n = 12
Output: "100"
```
After `"11"` at position 7, the strings of length 3 begin: `"000"` at 8, `"001"` at 9, `"010"` at 10, `"011"` at 11 and `"100"` at 12.
Overview: Binary strings are listed shortest first and in lexicographic order within each length, starting from the empty string. Return the n-th string for n up to a quadrillion. The problem tests counting strings by length, mapping an index to a string, and recognizing the structure of the ordering.
Read the full Quora Software Engineer interview experience this question came from
Consider the list of every string made of the characters `0` and `1`, ordered shortest first, with strings of the same length in lexicographic order: the empty string, `0`, `1`, `00`, `01`, `10`, `11`, `000`, `001`, and so on. Given an integer `n`, return the `n`-th string in this list.
Implement `nth_binary_string(n)`, which returns that string (the empty string when `n = 1`).
### Rules
- Positions are 1-indexed: `n = 1` is the empty string `""`, `n = 2` is `"0"`, and `n = 3` is `"1"`.
- Strings of equal length are ordered lexicographically, with `"0"` before `"1"`.
### Constraints
- `1 <= n <= 10^15`
- `n` can exceed the 32-bit integer range (2^31 - 1); use 64-bit integers in languages with fixed-width types (`long` in Java, `long long` in C++).
### Examples
**Example 1**
```text
Input: n = 6
Output: "10"
```
The list begins `""`, `"0"`, `"1"`, `"00"`, `"01"`, `"10"`, so the sixth string is `"10"`.
**Example 2**
```text
Input: n = 12
Output: "100"
```
After `"11"` at position 7, the strings of length 3 begin: `"000"` at 8, `"001"` at 9, `"010"` at 10, `"011"` at 11 and `"100"` at 12.
Constraints
- 1 <= n <= 10^15
- n can exceed the 32-bit integer range; use 64-bit integers in languages with fixed-width types (long in Java, long long in C++).
Examples
Input: (1,)
Expected Output: ''
Explanation: Minimum valid n (source example 1): position 1 is the empty string.
Input: (2,)
Expected Output: '0'
Explanation: First single-character string; off-by-one check on the 1-indexed start.
Hints
- Count how many strings of each length exist, and work out the position at which the strings of a given length begin.
- For two strings of the same length made of 0 and 1, how does their lexicographic order relate to their values read as binary numbers?
- Check your idea against the 1-indexed start: n = 1 must give the empty string, n = 2 gives "0" and n = 3 gives "1".