Solve Knight and Reversal Problems
UberSoftware Engineer · Online AssessmentLast updated
This question evaluates graph modeling, state-space exploration, and shortest-path/reachability reasoning for nonstandard movement rules and constrained state transformations.
The online assessment contained two coding problems:
-
Generalized knight shortest path: Given an integer
n, consider ann x nchessboard with coordinates from(0, 0)to(n-1, n-1). For each ordered pair of positive integers(a, b)where1 <= a, b < n, a piece can move from(x, y)to any of the eight positions formed by applying(+/-a, +/-b)or(+/-b, +/-a). For every(a, b), compute the minimum number of moves needed to reach(n-1, n-1)starting from(0, 0), or-1if the target is unreachable. -
Minimum segment reversals: Positions are numbered from
1ton. A token starts at positionp, and some positions are forbidden and cannot be occupied. In one move, you may choose any contiguous segment of lengthkthat contains the token, reverse that segment, and the token moves to its mirrored position inside the chosen segment. For every position from1ton, compute the minimum number of reversals required to move the token there, or-1if it cannot be reached. The assessment version used 1-indexed positions.
Solution walkthrough is for members
Get the full walkthrough with reference code and complexity analysis, plus Kitty Coach at 300 messages a day. Hints, the code runner and community answers stay free.