Magicsheet logo

Maximum Manhattan Distance After K Changes

Medium
12.5%
Updated 8/1/2025

Maximum Manhattan Distance After K Changes

What is this problem about?

The Maximum Manhattan Distance After K Changes coding problem gives you a string of directional moves (North, South, East, West) and a budget of K changes. You can change any K characters in the string to any direction. After all moves are executed, you want to maximize the Manhattan distance from the origin. This is an optimization problem combining string manipulation with coordinate geometry.

Why is this asked in interviews?

Uber, Microsoft, Meta, and Bloomberg use this problem to test candidates' ability to think about what "optimal" means in a spatial context. The key insight is that Manhattan distance from origin after n moves is maximized when moves are as aligned as possible (all in one axis direction or split optimally). Counting-based analysis and greedy allocation of change budget define the solution approach.

Algorithmic pattern used

The solution uses counting and greedy allocation. Count the moves in each direction: N, S, E, W counts. The net displacement on the y-axis is |N - S| and on x-axis is |E - W|. Total Manhattan distance is |N-S| + |E-W|. To maximize this, you want to "convert" opposing moves to the dominant direction. With K changes, you can convert opposing moves to the dominant direction, effectively doubling the impact of each change. The optimal strategy converts moves that cancel the dominant axis. Binary search or direct counting gives the maximum distance achievable with exactly K changes.

Example explanation

Moves: "NSEWS", K = 2.

  • Counts: N=1, S=1, E=1, W=1, S=1. So N=1, S=2, E=1, W=1.
  • Net y: |1-2|=1 (south-dominant). Net x: |1-1|=0.
  • Current distance = 1.
  • With K=2 changes: Convert 1 N (opposing south) to S → S=3, N=0. Net y = 3. Change 1 W to E or vice versa → net x = 2. Distance = 3+2 = 5? But total moves = 5, we changed 2.
  • Actually after 2 changes: S=3, N=0, E=2, W=0. Net = 3+2 = 5. Length of string = 5. Max distance = 5 (all moves in aligned directions). Answer = 5.

Common mistakes candidates make

  • Not considering both axes independently: Candidates often optimize only one axis, missing that changes can be split between axes optimally.
  • Changing moves outside the opposing direction: Only converting moves that oppose the dominant direction is optimal; changing neutral moves wastes the budget.
  • Off-by-one in change budget: Each change converts one opposing move to the dominant direction, adding 2 to the net (removes 1 from opposition, adds 1 to dominant).

Interview preparation tip

For the Math Hash Table Counting String interview pattern, frame this problem as: "I have cancelling pairs; each change I make eliminates one cancelling pair and adds one to the dominant direction." This +2 per change insight (not +1) is the key to the greedy calculation. Practice similar "maximize net displacement with K flips" problems to internalize this pattern.

Similar Questions