As reported: Apple ICT3 Full-Stack Engineer (Cupertino, 2025), technical phone screen, second question: a binary tree of 0s and 1s where a left child has the same value as its parent and a right child has the opposite value — find the value of the n-th node at a given level.
A full binary tree is built level by level. The root (level 1) is 0. Every node has two children: the left child has the same value as the node, the right child the opposite value.
Equivalently, each level is the previous one with every 0 replaced by 01 and every 1 by 10:
level 1: 0
level 2: 01
level 3: 0110
level 4: 01101001
Given n and k, return the value of the k-th node (1-indexed, left to right) on level n.
This is LeetCode 779 — K-th Symbol in Grammar.
Example 1
n = 1, k = 1 -> 0
Example 2
n = 2, k = 1 -> 0
n = 2, k = 2 -> 1
Constraints: 1 ≤ n ≤ 30; 1 ≤ k ≤ 2^(n − 1).
Code runner format: one JSON value per line: n, then k (LeetCode's test-case format). Implement kthGrammar(n, k); the harness prints the value.