238. Design Voter Management System for Conducting Elections
Asked in
Design Voter Management System for Conducting Elections
Design a voter management system for an election. The system must support creating the election structure using EVM details, registering voters, casting votes, and reading election results.

Each state is unique and can have multiple constituencies. Each constituency can have multiple districts. Each district can have many EVM machines for voting.

Each EVM belongs to one state, one constituency, and one district. Calling addEvm with valid location details should create the state, constituency, and district automatically if they do not already exist.
A voter can vote at most once. A vote is valid only when the voter is registered, the EVM exists in the voter's registered district, and the chosen candidate is available on that EVM.
When returning candidate results, sort candidates by higher vote count first. If two candidates have the same vote count, return the lexicographically smaller candidate name first.

Methods

Add EVM

boolean addEvm(String stateName, String constituencyName, String districtName, String evmId, List<String> candidateNames)
  • Adds an EVM to the given state, constituency, and district.
  • If the state, constituency, or district does not already exist, it is created automatically.
  • The EVM stores the list of candidates available for voting on that machine.
  • Returns true if the EVM was added.
  • Returns false if the EVM id already exists or the candidate list is empty.

Register Voter

boolean registerVoter(String voterId, String stateName, String constituencyName, String districtName)
  • Registers a voter in an existing district.
  • Returns true if the voter was registered.
  • Returns false if the district does not exist or the voter id is already registered.

Cast Vote

String castVote(String voterId, String evmId, String candidateName)
  • Casts one vote for a candidate from a registered voter.
  • Returns "SUCCESS" if the vote is accepted.
  • Returns "VOTER_NOT_FOUND" if the voter is not registered.
  • Returns "VOTER_ALREADY_VOTED" if the voter has already voted.
  • Returns "EVM_NOT_FOUND" if the EVM id does not exist.
  • Returns "INVALID_EVM_FOR_VOTER" if the EVM is not in the voter's registered district.
  • Returns "CANDIDATE_NOT_FOUND" if the candidate is not available on the EVM.
  • A rejected vote attempt does not mark the voter as voted.
  • Validation must happen in this order: voter existence, voter already voted, EVM existence, EVM district match, candidate availability.

Get Constituency Result

List<String> getConstituencyResult(String stateName, String constituencyName)
  • Returns the vote result for all candidates in the given constituency.
  • Each string must be formatted as "candidateName,voteCount".
  • Results are sorted by higher vote count first, then by lexicographically smaller candidate name.
  • Returns an empty list if the state or constituency does not exist.
  • If the same candidate name appears on multiple EVMs in the constituency, votes for that candidate are aggregated together.
  • Each candidate should appear at most once in the returned result.
  • Candidates available on any EVM in the constituency must be included even if they received zero votes.

Constraints

  • 1 ≤ stateName.length(), constituencyName.length(), districtName.length(), evmId.length(), voterId.length(), candidateName.length() ≤ 50
  • 1 ≤ candidateNames.size() ≤ 100
  • 1 ≤ total number of states ≤ 100
  • 1 ≤ total number of constituencies ≤ 10,000
  • 1 ≤ total number of districts ≤ 50,000
  • 1 ≤ total number of EVMs ≤ 100,000
  • 1 ≤ total number of voters ≤ 1,000,000
  • 1 ≤ total number of vote attempts ≤ 1,000,000
  • All input strings are non-empty and contain only letters, digits, spaces, hyphens, or underscores.
  • candidateNames will not contain duplicate candidate names for the same EVM.
  • All input parameters will be valid.

Examples

Example 1

VoterManagementSystem system = new VoterManagementSystem()
system.addEvm(stateName = "Bihar", constituencyName = "Patna Sahib", districtName = "Patna Urban", evmId = "EVM-101", candidateNames = List.of("Asha", "Biren", "Chitra"))
Output: true
system.registerVoter(voterId = "V001", stateName = "Bihar", constituencyName = "Patna Sahib", districtName = "Patna Urban")
Output: true
system.castVote(voterId = "V001", evmId = "EVM-101", candidateName = "Asha")
Output: "SUCCESS"
system.getConstituencyResult(stateName = "Bihar", constituencyName = "Patna Sahib")
Output: List.of("Asha,1", "Biren,0", "Chitra,0")
The EVM creates the election location automatically, the voter is registered in that district, and one valid vote is counted for Asha.

Example 2

VoterManagementSystem system = new VoterManagementSystem()
system.addEvm(stateName = "Karnataka", constituencyName = "Bengaluru Central", districtName = "MG Road", evmId = "EVM-501", candidateNames = List.of("Dev", "Meera"))
Output: true
system.registerVoter(voterId = "KA-77", stateName = "Karnataka", constituencyName = "Bengaluru Central", districtName = "MG Road")
Output: true
system.castVote(voterId = "KA-77", evmId = "EVM-501", candidateName = "Meera")
Output: "SUCCESS"
system.castVote(voterId = "KA-77", evmId = "EVM-501", candidateName = "Dev")
Output: "VOTER_ALREADY_VOTED"
system.getConstituencyResult(stateName = "Karnataka", constituencyName = "Bengaluru Central")
Output: List.of("Meera,1", "Dev,0")
The second vote from the same voter is rejected because each voter can vote only once.

Example 3

VoterManagementSystem system = new VoterManagementSystem()
system.addEvm(stateName = "Maharashtra", constituencyName = "Pune North", districtName = "Shivajinagar", evmId = "EVM-700", candidateNames = List.of("Nitin", "Ravi"))
Output: true
system.addEvm(stateName = "Maharashtra", constituencyName = "Pune North", districtName = "Kothrud", evmId = "EVM-701", candidateNames = List.of("Nitin", "Ravi"))
Output: true
system.registerVoter(voterId = "MH-11", stateName = "Maharashtra", constituencyName = "Pune North", districtName = "Kothrud")
Output: true
system.castVote(voterId = "MH-11", evmId = "EVM-700", candidateName = "Nitin")
Output: "INVALID_EVM_FOR_VOTER"
system.castVote(voterId = "MH-11", evmId = "EVM-701", candidateName = "Ravi")
Output: "SUCCESS"
system.getConstituencyResult(stateName = "Maharashtra", constituencyName = "Pune North")
Output: List.of("Ravi,1", "Nitin,0")
The voter is registered in Kothrud, so voting on the Shivajinagar EVM is rejected. Voting on the Kothrud EVM succeeds.


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