Fewest Jumps Across an Array With Same-Value Teleports

Last updated: November 29, 2026

Quick Overview

Starting at index 0 of an integer array, each step you may move to i + 1, i - 1, or any index j with arr[j] == arr[i]. Return the minimum number of steps to reach the last index.

Google
Coding & Algorithms
Software Engineer
Google
November 29, 2026
Software Engineer
Onsite
Coding & Algorithms
Hard

0

0

0 solved


Starting at index 0 of an integer array, each step you may move to i + 1, i - 1, or any index j with arr[j] == arr[i]. Return the minimum number of steps to reach the last index.

A Google onsite question where a straightforward BFS is quadratic because of the teleport edges. The interviewer is checking whether you notice that each value group only needs to be expanded once and can be deleted afterward.

What the Interviewer Expects
  • Model indices as nodes with neighbor and same-value edges
  • Group indices by value in a hash map
  • Use BFS for the minimum steps
  • Clear a value's index list after its first expansion
  • Explain why that keeps the algorithm linear
Key Topics to Cover
BFS
implicit graph
hash map grouping
edge pruning
How to Approach This
  1. Clarify input constraints and edge cases before writing code.
  2. Walk through your approach verbally and confirm with the interviewer before coding.
  3. Start with a brute force solution, then optimize. Mention time and space complexity.
  4. Test your solution with examples, including edge cases like empty input or duplicates.
  5. Consider common patterns: sliding window, two pointers, hash map, BFS/DFS, dynamic programming.
Possible Follow-up Questions
  • What if teleporting costs 2 steps while walking costs 1?
  • Use bidirectional BFS here; does it help?
  • Return the actual path.
Sharpen Your Skills on Codemia

Practice similar problems with our interactive workspace, get AI feedback, and track your progress.

Practice DSA Problems
Sample Answer
Requirements
  • Input: arr of length up to 5×1045 \times 10^4.
  • Moves: adjacent indices or any index with the same value.
  • Output: minimum steps from 0 to n - 1....
High-Level Design

BFS is right since all moves cost 1. The danger is the teleport edges: an array of nn equal values has n2n^2 such edges, and naive BFS would scan the full group from every member.

The fix: the first...


Submit Your Answer
Markdown supported