Nearest Charger Distances in One-Dimensional Arrays

Quick Overview

Solve global and per-position nearest-charger distance problems with linear scans. Compare two-pass preprocessing with repeated start-index queries, and keep the different array encodings and streaming limits explicit.

Nearest Charger Distances in One-Dimensional Arrays

Company: Spotify

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: hard

Interview Round: Onsite

Solve the following related one-dimensional distance tasks. Indices are zero-based and distance is absolute index difference. The reported variants use different encodings, so keep their interfaces separate. Numerical array-size bounds were not supplied. ### Part 1 — Closest Vehicle and Charger Anywhere An array contains `0` for empty, `1` for a vehicle, and `2` for a charger. Return the minimum distance over all vehicle-charger pairs. For `[0,1,0,0,2,0,1]`, return `2`. As a practice convention, return `-1` if either kind is absent. #### What This Part Should Cover Use a linear scan, explain why the most recently seen member of the opposite kind is sufficient, and account for empty cells. ### Part 2 — Nearest Opposite for Every Occupied Position Using the same `0/1/2` encoding, return the nearest-charger distance for every vehicle and the nearest-vehicle distance for every charger. Normalize the output to records `[index, distance]` in increasing index order, omitting empty cells and using `-1` when no opposite kind exists. The example above returns `[[1,3],[4,2],[6,2]]`. #### What This Part Should Cover Include opposite-kind positions on both sides. Explain why a scan that only knows past positions cannot immediately finalize every individual answer. ### Part 3 — Nearest Charger to a Supplied Start In this separate variant, `A` is an integer array in which value `1` marks a charger; other values are not chargers. Given a valid start index, return the nearest charger distance, or `-1` if no charger exists. An out-of-range start is invalid and must be rejected. The source explicitly includes these absent-charger and invalid-start rules. #### What This Part Should Cover Distinguish this encoding from Parts 1 and 2, and handle a charger at the start with distance zero. ### What a Strong Answer Covers Provide all three algorithms, their correctness arguments, and their complexity. Clearly distinguish a single global minimum from one answer per position, and explain which answers depend on future input. ### Follow-up Questions How would many start-index queries benefit from preprocessing? What changes if the array arrives as a stream and previous answers cannot be revised?

Overview: Solve global and per-position nearest-charger distance problems with linear scans. Compare two-pass preprocessing with repeated start-index queries, and keep the different array encodings and streaming limits explicit.

Spotify logo
Spotify
Oct 18, 2026
hardSoftware EngineerOnsiteCoding & Algorithms
0
0

Solve the following related one-dimensional distance tasks. Indices are zero-based and distance is absolute index difference. The reported variants use different encodings, so keep their interfaces separate. Numerical array-size bounds were not supplied.

Part 1 — Closest Vehicle and Charger Anywhere

An array contains 0 for empty, 1 for a vehicle, and 2 for a charger. Return the minimum distance over all vehicle-charger pairs. For [0,1,0,0,2,0,1], return 2. As a practice convention, return -1 if either kind is absent.

What This Part Should Cover Guidance

Use a linear scan, explain why the most recently seen member of the opposite kind is sufficient, and account for empty cells.

Part 2 — Nearest Opposite for Every Occupied Position

Using the same 0/1/2 encoding, return the nearest-charger distance for every vehicle and the nearest-vehicle distance for every charger. Normalize the output to records [index, distance] in increasing index order, omitting empty cells and using -1 when no opposite kind exists. The example above returns [[1,3],[4,2],[6,2]].

What This Part Should Cover Guidance

Include opposite-kind positions on both sides. Explain why a scan that only knows past positions cannot immediately finalize every individual answer.

Part 3 — Nearest Charger to a Supplied Start

In this separate variant, A is an integer array in which value 1 marks a charger; other values are not chargers. Given a valid start index, return the nearest charger distance, or -1 if no charger exists. An out-of-range start is invalid and must be rejected. The source explicitly includes these absent-charger and invalid-start rules.

What This Part Should Cover Guidance

Distinguish this encoding from Parts 1 and 2, and handle a charger at the start with distance zero.

What a Strong Answer Covers Guidance

Provide all three algorithms, their correctness arguments, and their complexity. Clearly distinguish a single global minimum from one answer per position, and explain which answers depend on future input.

Follow-up Questions Guidance

How would many start-index queries benefit from preprocessing? What changes if the array arrives as a stream and previous answers cannot be revised?