← Uber Interview Insights

Uber·Software Engineer·Online Assessment (OA)·Intermediate

Intermediate
Apr 2026

Summary

Uber SWE online assessment with two algorithmic problems. Both were on the harder side and felt more like competitive programming than typical interview prep stuff.

Questions Asked (2)

Q1

Given an n x n chessboard, a generalized knight can move using any ordered pair (a, b) where both values are between 1 and n-1, jumping in the 8 directions you'd expect from a normal knight. For every valid (a, b) pair, find the minimum number of moves to get from (0,0) to (n-1, n-1), or -1 if it's not reachable.

Algorithms & Data Structures
Author's notes

BFS is the right instinct here since you're looking for shortest path on an unweighted graph.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

Model the problem as a graph where each cell is a node and edges represent valid knight moves for a given (a,b). For each (a,b), run BFS from (0,0) to find the shortest path to (n-1,n-1). Optimize by precomputing move offsets and handling unreachable cases.

Pro tip: Mention that BFS is optimal for unweighted graphs, but also discuss potential optimizations like bidirectional BFS or A* with Manhattan distance heuristic to reduce search space, especially for large n.

1. Understand the problem and constraints

Clarify that n is the board size, (a,b) are move parameters with 1 ≤ a,b ≤ n-1, and moves are the 8 knight-like jumps. Note that each (a,b) is independent and we need the minimum moves for each.

2. Model as a graph and choose BFS

Represent each cell as a node. For a fixed (a,b), generate all valid moves from a cell. Since all moves have equal cost, BFS from (0,0) gives the shortest path to (n-1,n-1).

3. Implement BFS efficiently

Use a queue and a distance array (or visited set) to avoid revisiting cells. Precompute the 8 move offsets for the given (a,b) to avoid redundant calculations.

4. Handle edge cases and unreachable targets

If BFS exhausts all reachable cells without reaching (n-1,n-1), return -1. Also consider special cases like n=1 (start equals target, 0 moves) and when a or b equals 0 (not allowed by constraints).

5. Analyze complexity and potential optimizations

For each (a,b), BFS takes O(n^2) time and space. With O(n^2) pairs, total O(n^4) time. Discuss possible optimizations like bidirectional BFS or precomputing reachability for symmetric pairs.

Key Points to Mention

  • BFS guarantees shortest path in unweighted graphs.
  • Time complexity: O(n^4) for all (a,b) pairs, each BFS O(n^2).
  • Space complexity: O(n^2) for visited array and queue.
  • Edge cases: n=1, unreachable target, and symmetry (a,b) vs (b,a) may yield same results.
  • Optimization: bidirectional BFS can reduce search space.
  • Use of a 2D distance array initialized to -1 to track visited and distance.

AI-generated suggestions, not part of the candidate's original notes. May be inaccurate — verify before relying on them.

Q2

Positions 1 through n are arranged in a line. A token starts at position p, and some positions are forbidden. In one move, pick any contiguous segment of length k that contains the token, reverse it, and the token lands at its mirrored position within that segment. For every starting position, find the minimum reversals to reach each target position, or -1 if unreachable.

Algorithms & Data Structures
Author's notes

This one wrecked me a bit.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

Model the problem as a graph where each position is a node, and edges represent valid reversals that move the token from one position to another. Then run BFS from each starting position to compute shortest distances to all targets, skipping forbidden positions. Optimize by precomputing all possible moves for each position and using multi-source BFS if multiple starts share the same graph.

Pro tip: Clarify constraints first (n, k, number of queries) to choose the right algorithm; for large n, precompute moves and use BFS with early termination, and consider bidirectional BFS if only a few targets are needed.

1. Understand the move mechanics

Determine all valid reversals: for a token at position i, a segment of length k containing i can start at s where max(1, i-k+1) ≤ s ≤ min(i, n-k+1). The token moves to s + (s+k-1 - i) = 2s + k - 1 - i. Ensure the new position is not forbidden.

2. Build the graph

Create an adjacency list where each allowed position i has edges to all reachable positions j via a valid reversal, excluding forbidden positions. This graph is undirected because reversing the same segment again returns the token.

3. Run BFS for each start

For each starting position p (if not forbidden), perform BFS to compute the minimum number of moves to every other allowed position. If p is forbidden, all distances are -1. Use a queue and distance array initialized to -1.

4. Optimize for multiple queries

If there are many starting positions, precompute all-pairs shortest paths using BFS from each node (O(n*(n+m))). If n is large, consider that the graph may have structure (e.g., moves depend only on parity or modulo) and derive a formula or use bidirectional BFS for specific targets.

5. Handle edge cases

Check if k > n (no moves possible), if k = 1 (no movement), if the start is forbidden, and if the target is forbidden. Also consider disconnected components and unreachable targets.

Key Points to Mention

  • Graph modeling: positions as nodes, valid reversals as edges
  • BFS for shortest path in unweighted graph
  • Precomputation of moves for each position to avoid O(n) per move
  • Handling forbidden positions by excluding them from the graph
  • Time complexity: O(n * (n + m)) for all starts, where m is number of edges
  • Space complexity: O(n + m) for graph and O(n) for distances per BFS

AI-generated suggestions, not part of the candidate's original notes. May be inaccurate — verify before relying on them.