Katkat

Compute the Final Robot Score

NVIDIASoftware Engineer · Technical Screen

Last 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 x and y, where x <= y.
  • If x == y, both robots exhaust their energy and are permanently deactivated (removed from the fleet).
  • If x < y, the weaker robot x is deactivated, and the stronger robot y has its score reduced to y - x before 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 0 if no robots remain.

Examples

Example 1

Input: scores = [2, 7, 4, 1, 8, 1]
Output: 1

Explanation:

  1. The two highest scores are 7 and 8. They clash, and a robot with score 8 - 7 = 1 remains. Pool: [2, 4, 1, 1, 1].
  2. The two highest scores are 2 and 4. They clash, resulting in 4 - 2 = 2. Pool: [2, 1, 1, 1].
  3. The two highest scores are 2 and 1. They clash, resulting in 2 - 1 = 1. Pool: [1, 1, 1].
  4. The two highest scores are 1 and 1. Both are deactivated. Pool: [1].
  5. 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^5
  • 1 <= 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, returning 0.
    • Large values up to 10^9: ensure integer comparisons do not overflow in fixed-width languages.

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?