AppPerfect Interview Question for Software Engineers


Country: India
Interview Type: In-Person




Comment hidden because of low score. Click to expand.
2
of 2 vote

public static Main(string[] args)
{
	public VotingSystem votingSystem = VotingSystem.GetInstance();
	while(true)
	{
		// Add candidates
		break;
	}
	while(true)
	{
		// Add people vote.
	}
	var candidate = votingSystem.GetWinner();
	int winnerVoteCount = votingSystem.GetVoteCount(candidate);
}

public class VotingSystem
{
	private Dictionary<Candidate, int> candidatesVoteCount;
	private VotingSystem votingSystem;
	private VotingSystem()
	{
		candidatesVoteCount = GetExistingCandidateInformationFromDB();
	}
	public VotingSystem GetInstance()
	{
		if(votingSystem === null)
		{
			votingSystem =  new VotingSystem();
		}
		return votingSystem;
	}
	public bool AddCandidate(string name, PartyName partyName)
	{
		var candidate = new Candidate(name, partyName);
		if(candidatesVoteCount.ContainsKey(candidate))
		{
			throw new InvalidArgumentException("input candidate is already present");
		}
		candidatesVoteCount.Add(candidate, 0);
		return true;
	}
	public bool Remove(string name, PartyName partyName)
	{
		var candidate = new Candidate(name, partyName);
		if(candidatesVoteCount.ContainsKey(candidate))
		{
			candidatesVoteCount.Remove(candidate);
			return true;
		}
		throw new InvalidArgumentException("Input Candidate is not present in the system");
	}
	public bool AddVote(string name, PartyName partyName)
	{
		var candidate = new Candidate(name, partyName);
		if(candidatesVoteCount.ContainsKey(candidate))
		{
			candidatesVoteCount[candidate]++;
			return true;
		}
		throw new InvalidArgumentException("Input Candidate is not present in the system");
	}
	public Candidate GetWinner()
	{
		candidatesVoteCount.Aggregate((l, r) => l.Value > r.Value ? l : r).Key;
	}
	public int GetVoteCount(string name, PartyName partyName)
	{
		var candidate = new Candidate(name, partyName);
		if(candidatesVoteCount.ContainsKey(candidate))
		{
			return candidatesVoteCount[candidate];
		}
		throw new InvalidArgumentException("Input Candidate is not present in the system");
	}
	public int GetVoteCount(Candidate candidate)
	{
		if(candidatesVoteCount.ContainsKey(candidate))
		{
			return candidatesVoteCount[candidate];
		}
		throw new InvalidArgumentException("Input Candidate is not present in the system");
	}
	public GetTotalVoteCount()
	{
		return candidatesVoteCount.Sum(x => x.Value);
	}
	private Dictionary<Candidate, int> GetExistingCandidateInformationFromDB()
	{
		var map = new Dictionary<Candidate, int>();
		var rowSets = GetExistingCandidates(); // this method will call its internal db and return informations about the candidates
		foreach(Row row in rowSets)
		{
			map.Add(new Candidate(row[0], Enum.Parse(row[1], typeof(PartyName))),Int32.Parse(row[2]));
		}
		return map;
	}
}

public class Candidate
{
	public string name;
	public PartyName partyName;
	public Candidate(string name, PartyName partyName)
	{
		this.name = name;
		this.partyName = partyName;
	}
}

public Enum PartyName
{
	"a" = 1,
	"b" = 2
}

Complexities are
Add Candidate: O(1)
Add Vote: O(1)
Delete Candidate: O(1)
GetWinner : O(N), N is total candidate count // least used
GetCandidateVoteCount : O(1)
GetTotalVoteCount : O(N), can be made O(1) easily. It is scalable, can store data into the database and while we restart the machine, it can read data from the DB and initialize itself. We can very well optimize GetWinner call as well, to return output in O(1) time. To do that, we have to store, the candidate vote count in a LinkedList and in the dictionary, we will store candidate information and corresponding LinkedList node for the vote count. We will also store maxVoteCount variable outside of all these, and when the current candidate vote count become more then maxVoteCount, then we update maxVoteCount and put that node in the first of the LinkedList. By this way, we will be able to answer GetWinner as well in O(1), just that insert and delete will take more time not in terms of complexity but in terms of more computer operations.
Failure conditions are all taken care of.

- sonesh April 24, 2017 | Flag Reply


Add a Comment
Name:

Writing Code? Surround your code with {{{ and }}} to preserve whitespace.

Books

is a comprehensive book on getting a job at a top tech company, while focuses on dev interviews and does this for PMs.

Learn More

Videos

CareerCup's interview videos give you a real-life look at technical interviews. In these unscripted videos, watch how other candidates handle tough questions and how the interviewer thinks about their performance.

Learn More

Resume Review

Most engineers make critical mistakes on their resumes -- we can fix your resume with our custom resume review service. And, we use fellow engineers as our resume reviewers, so you can be sure that we "get" what you're saying.

Learn More

Mock Interviews

Our Mock Interviews will be conducted "in character" just like a real interview, and can focus on whatever topics you want. All our interviewers have worked for Microsoft, Google or Amazon, you know you'll get a true-to-life experience.

Learn More