Skip to the solution
MediumMachine Coding9 min read

Build a Poll / Voting widget

551 views

The problem

Question presented to candidate: "Build a small poll widget: a question, four options and a Vote button. After voting, the user sees each option's share as a bar with a percentage and a count, their own choice highlighted, and the total. A user must not be able to vote twice."

Requirements:

  • Options start with existing vote counts; the user picks one option, then submits.
  • The Vote button is disabled until something is selected.
  • After voting: results view with a bar, percentage and count per option, "your vote" marker and the total. Bars animate their width with CSS.
  • One vote only: the voting controls disappear, and the vote function itself refuses a second vote (even two clicks in the same tick).
  • Percentages are whole numbers that add up to exactly 100.

What a strong answer should cover:

  • Separating state (counts, chosen option, voted option) from derived values (total, percentages), which are computed, never stored.
  • Updating counts immutably and guarding the vote with the latest state, not a stale value.
  • Why Math.round can give 99 or 101 and how the largest remainder method fixes it.
  • Native radio inputs in a fieldset with a legend for free keyboard support and screen-reader grouping.
  • Results announced politely and bars marked decorative because the text already says the number.

Clarifying questions expected:

  • "Should results be visible before voting, or only after?"
  • "Can a user change their vote, or is it final?"
  • "Does the vote need to survive a reload or be saved to a server in this version?"

Code / implementation expected: Yes. A working widget with the single-vote rule and correct percentages.

  • React
  • State
  • Data

Try it yourself

Sketch your approach before you read the solution. That is what the interview tests.

Stuck? Get a hint.

A short nudge toward the approach. It will not give the answer away.

Solution

The solution is hidden so you can try first

9 min read Β· 9 sections

React solution

Every file of the working solution. Open it in the playground to run and change it.

ReactComplete solution
import Poll from "./src/components/Poll";

const OPTIONS = [
  { id: "react", label: "React", votes: 12 },
  { id: "vue", label: "Vue", votes: 7 },
  { id: "angular", label: "Angular", votes: 5 },
  { id: "svelte", label: "Svelte", votes: 3 },
];

export default function App() {
  return (
    <main className="app">
      <Poll
        question="Which framework do you reach for first?"
        options={OPTIONS}
      />
    </main>
  );
}

Discussion

Other approaches, follow-ups and how it went in real interviews.

Sign in to join the discussion.

No comments yet. Share how you would answer it.

Question 55 of 107 in Machine coding

Keep your momentum going.

Mark this question solved to track your progress, then take the next one while it is fresh.

Up nextMedium

Build a Job Board (paginated fetch)

Continue