Skip to content
QuizMaker logoQuizMaker
Activity
DSA Interview Patterns Roadmap

No lessons available

CONTENTS

Two Robots Minimum Distance

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

DSA Interview Patterns Roadmap
Company Asked Variants
dsa
coding interview
+5
May 29, 2026
20
A

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.

Share this article

Test your knowledge

Take a quick quiz based on this chapter.

hardDSA Interview Patterns
Quiz: Two Robots Minimum Distance
6 questions10 min

0 comments

Please login to comment.
No comments yet.
Lesson 8 of 13 in Company Asked Variants
Previous in Company Asked Variants
Arcesium Maximum Width Level
Next in Company Asked Variants
Two Stock Arrays with Switching Cost
Back to DSA Interview Patterns Roadmap
Back to moduleCategories