Katkat

Solve Knight and Reversal Problems

UberSoftware Engineer · Online Assessment

Last 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:

  1. Generalized knight shortest path: Given an integer n, consider an n x n chessboard with coordinates from (0, 0) to (n-1, n-1). For each ordered pair of positive integers (a, b) where 1 <= 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 -1 if the target is unreachable.

  2. Minimum segment reversals: Positions are numbered from 1 to n. A token starts at position p, and some positions are forbidden and cannot be occupied. In one move, you may choose any contiguous segment of length k that contains the token, reverse that segment, and the token moves to its mirrored position inside the chosen segment. For every position from 1 to n, compute the minimum number of reversals required to move the token there, or -1 if it cannot be reached. The assessment version used 1-indexed positions.