Enhancing Voting Systems with Advanced C++ Features

Introduction

Welcome back to a fascinating session where we will learn about enhancing existing functionality without causing regressions. Our scenario today involves designing a voting system. We'll start with the basic implementation of the voting system and gradually introduce additional elements of complexity.

Initial Solution Development

Let's start by building our basic voting system using C++ standard library features. We will utilize std::map to maintain our data, which allows us to seamlessly store and retrieve information about candidates and voters. This map acts as the backbone of our system for the dynamic association of keys to values.

#include <iostream>
#include <map>
#include <vector>
#include <algorithm>

class VotingSystem {
private:
    std::map<std::string, int> candidates_; // Stores candidate_id as key and votes as value
    std::map<std::string, std::pair<std::vector<std::string>, std::vector<int>>> voters_; // Tracks each voter's voting history

public:
    bool register_candidate(const std::string& candidate_id) {
        // Add a new candidate to the system if not already present
        if (candidates_.count(candidate_id)) {
            return false; // Candidate is already registered
        }
        candidates_[candidate_id] = 0; // Initialize candidate with 0 votes
        return true;
    }

    bool vote(int timestamp, const std::string& voter_id, const std::string& candidate_id) {
        // Cast a vote if the candidate is registered
        if (!candidates_.count(candidate_id)) {
            return false; // Candidate not registered
        }
        voters_[voter_id].first.push_back(candidate_id); // Record the vote
        voters_[voter_id].second.push_back(timestamp); // Record the time of the vote
        candidates_[candidate_id]++; // Increment vote count for the candidate
        return true;
    }

    int get_votes(const std::string& candidate_id) const {
        // Retrieve vote count for a candidate or return -1 if not found
        auto it = candidates_.find(candidate_id);
        if (it != candidates_.end()) {
            return it->second;
        }
        return -1;
    }
};

Introduce New Methods

Now that we have a basic voting system, our goal is to enhance this system with additional functionalities:

  • get_voting_history(const std::string& voter_id): Provides a detailed voting history for a specified voter, returning a map that shows how many times the voter has voted for each candidate.
  • block_voter_registration(int timestamp): Implements a mechanism to halt any new voter registrations past a specified timestamp, effectively freezing the voter list as of that moment.
  • change_vote(int timestamp, const std::string& voter_id, const std::string& old_candidate_id, const std::string& new_candidate_id): Enables voters to change their vote from one candidate to another, given the change is made within a 24-hour window from their last vote, ensuring both the old and new candidates are registered, and that the voter initially voted for the old candidate.
Sign up

Join the 1M+ learners on CodeSignal

Be a part of our community of 1M+ users who develop and demonstrate their skills on CodeSignal