#3709
Medium Algorithms Design exam scores tracker
Array Binary Search Design Prefix Sum
43.9% acceptance
Feb 24, 2026
58
1
Alice frequently takes exams and wants to track her scores and calculate the total scores over specific time periods.
Implement the ExamTracker class:
ExamTracker(): Initializes the ExamTracker object.
void record(int time, int score): Alice takes a new exam at time time and achieves the score score.
long long totalScore(int startTime, int endTime): Returns an integer that represents the total score of all exams
taken by Alice between startTime and endTime (inclusive). If there are no recorded exams taken by Alice within the
specified time interval, return 0.
It is guaranteed that the function calls are made in chronological order. That is,
Calls to record() will be made with strictly increasing time.
Alice will never ask for total scores that require information from the future. That is, if the latest record() is
called with time = t, then totalScore() will always be called with startTime <= endTime <= t.
Solution
Rust
Time O(log n)
Space O(n)
pub struct ExamTracker {
times: Vec<i32>,
prefix: Vec<i64>,
}
impl ExamTracker {
pub fn new() -> Self {
ExamTracker {
times: vec![],
prefix: vec![0],
}
}
pub fn record(&mut self, time: i32, score: i32) {
self.times.push(time);
let last = *self.prefix.last().unwrap();
self.prefix.push(last + score as i64);
}
pub fn total_score(&self, start_time: i32, end_time: i32) -> i64 {
let lo = self.times.partition_point(|&t| t < start_time);
let hi = self.times.partition_point(|&t| t <= end_time);
self.prefix[hi] - self.prefix[lo]
}
}