220. Design Vote Share Display for Voting System
Design MCQ Voting System
Design a voting system for a multiple-choice question with exactly 4 options. When a user clicks one option, that option receives one vote. After every vote, the system should return the updated vote percentage for all options.
The percentage of each option also represents how much color fill should be shown for that option in the UI. For example, if an option has 40% votes, then 40% of that option row should be filled with color.
Percentages must be deterministic. Use integer percentage values calculated using floor division: percentage = votesForOption * 100 / totalVotes. Return options in their original order.

Because floor division is used, returned percentages are not required to sum to exactly 100.

Method Signatures

Constructor

VotingSystem(List<String> options)
  • Initializes the voting system with exactly 4 MCQ options.
  • All options start with 0 votes.

Record Vote

List<String> vote(int optionIndex)
  • Records one vote for the option at optionIndex.
  • Returns the current state of all options after the vote.
  • Each returned string must be in the format "optionText,votes,percentage".
  • The percentage value is also the color fill percentage for that option.

Get Current Result

List<String> getResults()
  • Returns the current voting result without adding a new vote.
  • Each returned string must be in the format "optionText,votes,percentage".
  • If no vote has been cast yet, every option should have 0%.

Constraints

  • options.size() = 4
  • 1 ≤ options.get(i).length() ≤ 100
  • 0 ≤ optionIndex < 4
  • 0 ≤ total number of method calls ≤ 100,000
  • Option texts are non-empty strings and do not contain commas.
  • All inputs are guaranteed to be valid.
  • Returned results must always follow the original option order.

Examples

Example 1

VotingSystem(options = ["Java", "Python", "C++", "JavaScript"])
getResults()
Output: ["Java,0,0", "Python,0,0", "C++,0,0", "JavaScript,0,0"]
No vote has been cast yet, so every option has 0 votes and 0% color fill.

Example 2

VotingSystem(options = ["Red", "Blue", "Green", "Yellow"])
vote(optionIndex = 1)
Output: ["Red,0,0", "Blue,1,100", "Green,0,0", "Yellow,0,0"]
One vote is added to "Blue". It has all votes, so its percentage and color fill become 100%.

Example 3

VotingSystem(options = ["A", "B", "C", "D"])
vote(optionIndex = 0)
Output: ["A,1,100", "B,0,0", "C,0,0", "D,0,0"]
vote(optionIndex = 2)
Output: ["A,1,50", "B,0,0", "C,1,50", "D,0,0"]
vote(optionIndex = 2)
Output: ["A,1,33", "B,0,0", "C,2,66", "D,0,0"]
After three votes, option "A" has 1 vote and option "C" has 2 votes. Percentages are calculated using floor division.


Please use Laptop/Desktop or any other large screen to add/edit code.