Article start
DSA Interview Patterns Roadmap
Company Asked Variants

Two Robots Minimum Distance

Turn the Two Robots Minimum Distance interview variant into a clear brute-force baseline, optimized pattern, and implementation plan.

May 29, 2026·21

Learning Outcome

Turn the Two Robots Minimum Distance interview variant into a clear brute-force baseline, optimized pattern, and implementation plan.

Original Interview Statement

Given ordered queries from point a to b, two robots can execute them. Minimize total movement.

Examples

ItemDetail
queries = [(1,5),(3,2),(4,1),(2,4)]minimum total depends on starting convention

Brute Force Approach

Assign each query to robot 1 or robot 2 recursively, producing 2^q choices.

Optimized Approach

DP by query index and the idle robot position; the active robot is implied by the previous query end.

Exact Pseudocode

dp(i, idle)
active = end of query i-1
option1 = move active to query i start/end
option2 = move idle to query i start/end
return min

Reference Code

from functools import lru_cache

def minimum_distance(queries):
    n = len(queries)

    @lru_cache(None)
    def dp(i, idle):
        if i == n:
            return 0
        start, end = queries[i]
        active = None if i == 0 else queries[i - 1][1]
        move_active = abs(start - end) if active is None else abs(active - start) + abs(start - end)
        best = move_active + dp(i + 1, idle)
        if idle != -1:
            move_idle = abs(idle - start) + abs(start - end)
            best = min(best, move_idle + dp(i + 1, active if active is not None else -1))
        else:
            best = min(best, abs(start - end) + dp(i + 1, active if active is not None else -1))
        return best

    return dp(0, -1)

Complexity

ItemDetail
Brute forceO(2^q)
OptimizedO(q^2) states

Edge Cases

  • First query has no active robot
  • Same start/end
  • Large coordinates

Follow-ups

  • Return assignment path
  • More than two robots

Nearest Practice References

  • HackerRank Two Robots
  • DP with compressed state

Common Mistakes

  • Copying the nearest LeetCode solution without checking the changed rule.
  • Skipping duplicate or boundary cases.
  • Not stating the brute force before the optimized approach.

Discussion

0 comments

Sign in to share a question or add to the discussion.
Start the discussion

Ask a question or share what stood out to you.