↓Skip to main content
  1. LeetCode/

LeetCode 3709: Design Exam Scores Tracker

·2 mins· ·
LeetCode Medium Binary-Search Prefix-Sum Design
Wei Yi Chung
Author
Wei Yi Chung
Working at the contributing of open source, distributed systems, and data engineering.
Table of Contents

Meta Data
#

Difficulty: medium First Attempt: 2025-10-11

  • Submissions: Wrong Answer → Runtime Error → Wrong Answer → Accepted

Approach
#

record times are strictly increasing, so the time array is already sorted.

  • times: the time of every record.
  • prefix: prefix sums of scores, where prefix[i] is the total of the first i records and prefix[0] = 0.

For a query [start, end]:

left  = first position with time >= start = bisect_left(times, start)
right = first position with time >  end   = bisect_right(times, end)
total = prefix[right] - prefix[left]

The leading 0 in prefix removes the special case where the left boundary is 0.

My Attempts
#

WA and RE: mid without parentheses
#

mid = left + right // 2       # means left + (right // 2)

// binds tighter than +, so mid overshoots: sometimes it finds the wrong position (WA), sometimes it goes out of range (RE).

WA: a query range with no records
#

After fixing mid, a range with no exams produced a left boundary larger than the right one, giving negative totals or bad indices. Several boundary checks were added before it passed.

The root cause was a prefix array without a leading 0, which forced special cases for “is the left boundary 0”. With prefix[0] = 0 and two half-open boundaries, none of those checks are needed: with no records the two boundaries are equal, so the difference is 0.

Solution
#

import bisect

class ExamTracker:

    def __init__(self):
        self.times = []
        self.prefix = [0]

    def record(self, time: int, score: int) -> None:
        self.times.append(time)
        self.prefix.append(self.prefix[-1] + score)

    def totalScore(self, startTime: int, endTime: int) -> int:
        lo = bisect.bisect_left(self.times, startTime)
        hi = bisect.bisect_right(self.times, endTime)
        return self.prefix[hi] - self.prefix[lo]

Complexity
#

record is O(1), totalScore is O(log n). Space O(n).

Mistakes to Avoid
#

  • mid = left + right // 2: missing parentheses.
  • A prefix array without a leading 0, leading to many special cases.
  • Not returning 0 when the range has no records.

Interview Explanation
#

Record times are strictly increasing, so I store the times and prefix sums of scores, with a leading 0 in the prefix. A query uses bisect_left for the first time at or after the start and bisect_right for the first time after the end; the prefix difference is the total, and with no records the two positions coincide, giving 0. Records are O(1), queries O(log n).

Related

LeetCode 3508: Implement Router
·3 mins
LeetCode Medium Design Binary-Search Queue Hash-Map
LeetCode 3508 note: a queue for FIFO, a set for duplicates, and an increasing time list per destination queried with bisect
LeetCode 981: Time Based Key-Value Store
·2 mins
LeetCode Medium Hash-Table String Binary-Search Design
LeetCode 981 note: store a (timestamp, value) list per key, with timestamps increasing; get binary searches the last timestamp <= the query
LeetCode 209: Minimum Size Subarray Sum (Sliding Window)
·1 min
LeetCode Daily Medium Array Sliding-Window Two-Pointers Prefix-Sum Binary-Search
Sliding window with two pointers; minimize subarray length where sum ≥ target.
LeetCode 2874: Maximum Value of an Ordered Triplet II
·2 mins
LeetCode Medium Prefix-Sum Array
LeetCode 2874 note: fix the middle j and take the prefix maximum on the left and the suffix maximum on the right, or scan once tracking the maximum and the best difference
LeetCode 1248: Count Number of Nice Subarrays
·3 mins
LeetCode Medium Sliding-Window Prefix-Sum Hash-Table Array
LeetCode 1248 note: “exactly k odd numbers” is not monotonic, so compute “at most k” minus “at most k - 1”; or use prefix sums with a hash map
LeetCode 3613: Minimize Maximum Component Cost
·2 mins
LeetCode Medium Union-Find Graph Sorting Binary-Search
LeetCode 3613 note: add edges from light to heavy; the weight when exactly k components remain is the answer, and 0 when n <= k