Watch 10 video solutions for Online Election, a medium level problem involving Array, Hash Table, Binary Search. This walkthrough by Happy Coding has 2,688 views views. Want to try solving it yourself? Practice on FleetCode or read the detailed text solution.
You are given two integer arrays persons and times. In an election, the ith vote was cast for persons[i] at time times[i].
For each query at a time t, find the person that was leading the election at time t. Votes cast at time t will count towards our query. In the case of a tie, the most recent vote (among tied candidates) wins.
Implement the TopVotedCandidate class:
TopVotedCandidate(int[] persons, int[] times) Initializes the object with the persons and times arrays.int q(int t) Returns the number of the person that was leading the election at time t according to the mentioned rules.
Example 1:
Input ["TopVotedCandidate", "q", "q", "q", "q", "q", "q"] [[[0, 1, 1, 0, 0, 1, 0], [0, 5, 10, 15, 20, 25, 30]], [3], [12], [25], [15], [24], [8]] Output [null, 0, 1, 1, 0, 0, 1] Explanation TopVotedCandidate topVotedCandidate = new TopVotedCandidate([0, 1, 1, 0, 0, 1, 0], [0, 5, 10, 15, 20, 25, 30]); topVotedCandidate.q(3); // return 0, At time 3, the votes are [0], and 0 is leading. topVotedCandidate.q(12); // return 1, At time 12, the votes are [0,1,1], and 1 is leading. topVotedCandidate.q(25); // return 1, At time 25, the votes are [0,1,1,0,0,1], and 1 is leading (as ties go to the most recent vote.) topVotedCandidate.q(15); // return 0 topVotedCandidate.q(24); // return 0 topVotedCandidate.q(8); // return 1
Constraints:
1 <= persons.length <= 5000times.length == persons.length0 <= persons[i] < persons.length0 <= times[i] <= 109times is sorted in a strictly increasing order.times[0] <= t <= 109104 calls will be made to q.Problem Overview: You receive two arrays: persons and times. Each vote is cast for persons[i] at time times[i]. After preprocessing the votes, you must answer queries q(t) that return the candidate leading at time t. If multiple candidates tie, the most recent vote wins.
Approach 1: Vote Counting with On-Demand Search (O(n) per query time, O(n) space)
For each query, first locate the last vote that occurred at or before time t. Since times is sorted, use binary search to find this index in O(log n). Then iterate from index 0 to that position and count votes using a hash map. Track the candidate with the highest frequency and apply the tie‑breaking rule by preferring the most recent vote. This approach uses hash tables for counting and binary search for locating the relevant vote window.
The downside is repeated work. Every query recomputes vote counts from scratch, giving O(n) processing per query and O(n) extra space for the frequency map. It works when the number of queries is small or when simplicity matters more than query performance.
Approach 2: Precomputation Using Binary Search (O(n) preprocessing, O(log n) per query, O(n) space)
Instead of recomputing results for every query, compute the leader after each vote during initialization. Iterate once through persons, maintain a frequency map of votes, and track the current leader. Whenever a candidate’s vote count becomes greater than or equal to the current leader’s count, update the leader (this naturally handles the tie-breaking rule because the later vote wins). Store the leader at each index in a separate array.
When a query q(t) arrives, use binary search on the times array to find the last vote that occurred before or at t. The answer is simply the precomputed leader stored at that index. Query time becomes O(log n) and no recounting is required. This design combines prefix-style preprocessing with efficient lookups and fits well with problems involving historical state queries.
The technique relies on arrays for storing prefix leaders, hash tables for vote counts, and binary search for fast time lookup. Conceptually, this also falls under design problems because the class must support efficient repeated queries.
Recommended for interviews: The precomputation approach is what interviewers expect. It demonstrates that you recognize repeated queries and move expensive work to preprocessing. The on-demand counting approach shows baseline understanding, but the binary-search + prefix leader design shows stronger algorithmic thinking and system design awareness.
| Approach | Time | Space | When to Use |
|---|---|---|---|
| Vote Counting with On-Demand Search | O(n) per query | O(n) | When queries are few and implementation simplicity is preferred |
| Precomputation Using Binary Search | O(n) build + O(log n) per query | O(n) | Best for many queries; avoids recomputing vote counts |