#C2170. Q&A Platform Simulation
Q&A Platform Simulation
Q&A Platform Simulation
You are required to simulate a simplified online Q&A platform. In this simulation, users can post questions, submit answers to questions, upvote or downvote answers, and retrieve the top answer for a specified question.
The platform supports the following commands:
postQuestion userId questionId
: Create a new question with id questionId by the user with id userId. If the question already exists, do nothing.submitAnswer userId questionId answerId
: Submit an answer with id answerId for the question questionId by the user with id userId. Note: If the question does not exist, it should be implicitly created.upvoteAnswer answerId
: Upvote the answer with id answerId (increase its vote count by 1).downvoteAnswer answerId
: Downvote the answer with id answerId (decrease its vote count by 1).getTopAnswer questionId
: Retrieve and output the answer id with the highest vote count for the question questionId. If no answer exists for the question, outputNone
.
The vote count of an answer is the net sum of upvotes and downvotes. When determining the top answer, if multiple answers have the same vote count, any one of them is acceptable.
Mathematical formulation:
If we denote the vote count of an answer with id a as \(V(a)\), then for a given question \(q\) having a set of answers \(A(q)\), the top answer is
\[
a^* = \operatorname*{argmax}_{a \in A(q)} V(a).
\]
inputFormat
The first line contains an integer n
representing the number of commands. The following n
lines each contain one command. The commands are in one of the following formats:
postQuestion userId questionId
submitAnswer userId questionId answerId
upvoteAnswer answerId
downvoteAnswer answerId
getTopAnswer questionId
All numeric values are integers. For every getTopAnswer
command, your program should output the result immediately.
outputFormat
For each getTopAnswer
command in the input, output the answer id with the highest vote count for the given question. If no answer exists for the question, output None
. Each result should be printed on a new line.
7
postQuestion 1 40
submitAnswer 2 40 15
submitAnswer 3 40 20
upvoteAnswer 15
upvoteAnswer 20
upvoteAnswer 20
getTopAnswer 40
20