← Questions
Score an array by taking the smallest and marking its neighbours
0
I was asked this
No reports yet. Sign in to say where you were asked it.
A coding interview question asked in Amazon's technical phone screen, last asked Jan 2026. It tests heap, greedy and arrays.
Given an integer array, repeat one step until every element is deleted: take the undeleted element with the smallest value (the leftmost one on a tie), add its value to the score, and delete it together with its immediate neighbours in the original array (indices i - 1 and i + 1) if they are not deleted yet. Return the final score.
Interface
Unless you agree on a different interface with the interviewer, assume:
min_delete_score(nums: list[int]) -> int
Example
min_delete_score([3, 1, 5, 2, 4]) -> 3 # take 1 (deletes indices 0-2), then 2 (deletes 3-4)
min_delete_score([1, 1, 1]) -> 2 # leftmost 1 deletes indices 0-1, then the last 1
min_delete_score([2, 1, 3, 4, 5, 2]) -> 7 # 1, then the 2 at index 5, then 4
min_delete_score([]) -> 0
Constraints
-
0 <= len(nums) <= 2 * 10^5;-10^9 <= nums[i] <= 10^9. - The score can exceed 32 bits.
Clarifying questions
- Are neighbours taken from the original positions, or from the elements still left? (Original positions.)
- If a neighbour is already deleted, is anything else deleted in its place? (No.)
- Can values be negative?
What a strong answer covers
- Sorting the indices once by
(value, index)and walking that order, skipping indices already deleted, with a boolean array instead of removing from the list: O(n log n) time, O(n) space. - Equivalently a min-heap of
(value, index)with lazy deletion. - Why the order never needs updating: deleting elements never changes the value or index of the rest.
- Ties broken by index, empty input, a single element, 64-bit sums.
Follow-ups
- Delete the
dneighbours on each side instead of one. - Neighbours are the nearest elements still undeleted rather than the original positions. What structure do you need now?
Where it's asked
- Amazon · Technical phone screen · Last asked Jan 202660 min · Live coding in a shared editor that does not run code, plus Leadership Principles questions · No AI
What it tests
No official answer yet. Write yours on the right and run it.
Nothing published yet.
Sign in to submit your code; every attempt is kept here.
Write and test your solution herePython in your browser, the question's tests level by level, every submission kept for you.Sign in to start coding
