Overflow in intermediate arithmetic
Wrong move: Temporary multiplications exceed integer bounds.
Usually fails on: Large inputs wrap around unexpectedly.
Fix: Use wider types, modular arithmetic, or rearranged operations.
Move from brute-force thinking to an efficient approach using math strategy.
We build a table of n rows (1-indexed). We start by writing 0 in the 1st row. Now in every subsequent row, we look at the previous row and replace each occurrence of 0 with 01, and each occurrence of 1 with 10.
n = 3, the 1st row is 0, the 2nd row is 01, and the 3rd row is 0110.Given two integer n and k, return the kth (1-indexed) symbol in the nth row of a table of n rows.
Example 1:
Input: n = 1, k = 1 Output: 0 Explanation: row 1: 0
Example 2:
Input: n = 2, k = 1 Output: 0 Explanation: row 1: 0 row 2: 01
Example 3:
Input: n = 2, k = 2 Output: 1 Explanation: row 1: 0 row 2: 01
Constraints:
1 <= n <= 301 <= k <= 2n - 1Problem summary: We build a table of n rows (1-indexed). We start by writing 0 in the 1st row. Now in every subsequent row, we look at the previous row and replace each occurrence of 0 with 01, and each occurrence of 1 with 10. For example, for n = 3, the 1st row is 0, the 2nd row is 01, and the 3rd row is 0110. Given two integer n and k, return the kth (1-indexed) symbol in the nth row of a table of n rows.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Math · Bit Manipulation
1 1
2 1
2 2
Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #779: K-th Symbol in Grammar
class Solution {
public int kthGrammar(int n, int k) {
if (n == 1) {
return 0;
}
if (k <= (1 << (n - 2))) {
return kthGrammar(n - 1, k);
}
return kthGrammar(n - 1, k - (1 << (n - 2))) ^ 1;
}
}
// Accepted solution for LeetCode #779: K-th Symbol in Grammar
func kthGrammar(n int, k int) int {
if n == 1 {
return 0
}
if k <= (1 << (n - 2)) {
return kthGrammar(n-1, k)
}
return kthGrammar(n-1, k-(1<<(n-2))) ^ 1
}
# Accepted solution for LeetCode #779: K-th Symbol in Grammar
class Solution:
def kthGrammar(self, n: int, k: int) -> int:
if n == 1:
return 0
if k <= (1 << (n - 2)):
return self.kthGrammar(n - 1, k)
return self.kthGrammar(n - 1, k - (1 << (n - 2))) ^ 1
// Accepted solution for LeetCode #779: K-th Symbol in Grammar
struct Solution;
impl Solution {
fn kth_grammar(n: i32, k: i32) -> i32 {
let n = n as usize - 1;
let k = k as usize - 1;
Self::kth(n, k)
}
fn kth(n: usize, k: usize) -> i32 {
if n == 0 {
0
} else {
Self::kth(n - 1, k / 2) ^ (k % 2) as i32
}
}
}
#[test]
fn test() {
let n = 1;
let k = 1;
let res = 0;
assert_eq!(Solution::kth_grammar(n, k), res);
let n = 2;
let k = 1;
let res = 0;
assert_eq!(Solution::kth_grammar(n, k), res);
let n = 2;
let k = 2;
let res = 1;
assert_eq!(Solution::kth_grammar(n, k), res);
let n = 4;
let k = 5;
let res = 1;
assert_eq!(Solution::kth_grammar(n, k), res);
let n = 30;
let k = 417_219_134;
let res = 1;
assert_eq!(Solution::kth_grammar(n, k), res);
}
// Accepted solution for LeetCode #779: K-th Symbol in Grammar
function kthGrammar(n: number, k: number): number {
if (n == 1) {
return 0;
}
if (k <= 1 << (n - 2)) {
return kthGrammar(n - 1, k);
}
return kthGrammar(n - 1, k - (1 << (n - 2))) ^ 1;
}
Use this to step through a reusable interview workflow for this problem.
Sort the array in O(n log n), then scan for the missing or unique element by comparing adjacent pairs. Sorting requires O(n) auxiliary space (or O(1) with in-place sort but O(n log n) time remains). The sort step dominates.
Bitwise operations (AND, OR, XOR, shifts) are O(1) per operation on fixed-width integers. A single pass through the input with bit operations gives O(n) time. The key insight: XOR of a number with itself is 0, which eliminates duplicates without extra space.
Review these before coding to avoid predictable interview regressions.
Wrong move: Temporary multiplications exceed integer bounds.
Usually fails on: Large inputs wrap around unexpectedly.
Fix: Use wider types, modular arithmetic, or rearranged operations.