Compute the Final Robot Score
NVIDIASoftware Engineer · Technical ScreenLast updated
Tests proficiency in using priority queues (heaps) to manage dynamic multisets under iterative reductions. Evaluates the ability to simulate priority-based selection and reason about time and space complexity.
Problem Statement
You are evaluating a fleet of autonomous robots undergoing combat stress testing. You are given an array of positive integers scores, where scores[i] represents the initial performance score of the i-th robot.
In each round of the simulation, the two robots with the highest scores are chosen to compete:
- Let the two highest scores be
xandy, wherex <= y. - If
x == y, both robots exhaust their energy and are permanently deactivated (removed from the fleet). - If
x < y, the weaker robotxis deactivated, and the stronger robotyhas its score reduced toy - xbefore returning to the fleet.
This process repeats until at most one active robot remains. Return the score of the final remaining robot, or 0 if all robots have been deactivated.
Function Signature
def compute_final_robot_score(scores: list[int]) -> int:
pass
Input & Output Format
- Input: A list of integers
scores(1 <= len(scores) <= 10^5,1 <= scores[i] <= 10^9). - Output: An integer representing the final robot score, or
0if no robots remain.
Examples
Example 1
Input: scores = [2, 7, 4, 1, 8, 1]
Output: 1
Explanation:
- The two highest scores are
7and8. They clash, and a robot with score8 - 7 = 1remains. Pool:[2, 4, 1, 1, 1]. - The two highest scores are
2and4. They clash, resulting in4 - 2 = 2. Pool:[2, 1, 1, 1]. - The two highest scores are
2and1. They clash, resulting in2 - 1 = 1. Pool:[1, 1, 1]. - The two highest scores are
1and1. Both are deactivated. Pool:[1]. - Only one robot remains with score
1.
Example 2
Input: scores = [5, 5]
Output: 0
Explanation: Both robots have score 5. They destroy each other, leaving 0 robots.
Constraints & Edge Cases
1 <= len(scores) <= 10^51 <= scores[i] <= 10^9- Edge cases to consider:
- Single element array (
len(scores) == 1): return the element immediately. - All scores are equal and
len(scores)is even: all robots cancel out, returning0. - Large values up to
10^9: ensure integer comparisons do not overflow in fixed-width languages.
- Single element array (
Hints
Hint 1
Consider what data structure allows you to repeatedly extract the maximum element and insert new elements in logarithmic time.
Hint 2
Python's standard heapq module implements a min-heap by default; you can negate values to simulate a max-heap.
Hint 3
In each step, at least one robot is eliminated, guaranteeing that the simulation terminates in at most n - 1 rounds.
Follow-up questions
- How would you optimize this if scores are bounded such that 1 <= scores[i] <= 1000, but n is very large (e.g., 10^7)?
- What if robot ties are broken by their original index instead of arbitrary order? How would you adjust your heap elements?
- Suppose scores can be added dynamically in real time via a stream while the reduction runs. How would you design a thread-safe solution?
Solution walkthrough is for members
Get the full walkthrough with reference code and complexity analysis, plus Kitty Coach at 300 messages a day. Hints, the code runner and community answers stay free.