If I understand everything, votes
is a list of vote tuples that are made up of candidates that are being voted for and the timestamp of the vote taking place. currTime
is the timestamp of all of the votes during that timestamp of before it. topCandidates
are the candidates with the highest vote at currTime
.
Your first question gives you votes
and currTime
, you are expected to return topCandidates
. Your second question gives you votes
and topCandidates
, and you are expected to return currTime
.
Focusing on the second question, I would make a map where the keys are a timestamp and the values are all of the votes taking place at that moment. Also I would create another map where the key is the candidate and the value is the number of votes they have so far. I would traverse the first map in the ascending timestamp order of the first map, get all of the votes that were cast at the timestamp and increment the second map's values by their candidate (key). Before going through the next timestamp, I would create a list of the most voted for candidates with the data in the second map. If that list matches topCandidates
, then the last timestamp you traversed through is currTime
.
To code this in python:
from collections import Counter, defaultdict
def findCurrTime(votes, topCandidates):
if not (votes and topCandidates):
return -1
votesAtTime = defaultdict(list)
candidatePoll = Counter()
k = len(topCandidates)
for time, candidate in votes: # votes = [(time0, candidate0), ...]
votesAtTime[time].append(candidate)
for ts in votesAtTime:
candidatePoll += Counter(voteAtTime[ts])
if list(map(lambda pair: pair[0],candidatePoll.most_common(k))) == topCandidates:
return ts
# if topCandidates cannot be created from these votes:
return -1
There are some assumptions that I've made (that you hopefully asked your interviewer about). I assumed that the order of topCandidates
mattered which Counter.most_common
handled, although it won't handle candidates with number of votes.
The time complexity is O(t * n * log(k)) with t being the number of timestamps, n being the number of votes and k being the size of topCandidates
. This is because Counter.most_common
looks to be O(n*log(k)) and it can run t
times. There are definitely more efficient answers though.