Start learning free

LESSON 129 OF 129 · AI ENGINEERING: INTERVIEW QUESTIONS

Ask Three Times, Take the Majority in Python

Practice

The interview question: “Your model labels support tickets, but the same ticket sometimes gets a different label. How do you make the label more reliable, and what does it cost?” The expected answer: ask the model 3 times and take the majority, the label with at least 2 of the 3 votes. If all three disagree, return a fixed fallback instead of guessing. It costs 3 times the calls.

Counting votes takes a dict from label to count. An example with a spam filter:

votes ["spam", "ok", "spam"]   →  {"spam": 2, "ok": 1}              →  spam (2 of 3)
votes ["spam", "ok", "promo"]  →  {"spam": 1, "ok": 1, "promo": 1}  →  no majority: fallback

The starting code has the client, the SYSTEM prompt, FALLBACK = "human", three tickets and classify(ticket): one model call, the reply stripped and lowercased, its tokens added to tokens. The course model answers from a script, so the votes are the same on every run. When vote is done, the program prints the lines below, up to Calls; the check then votes on one more ticket and counts the model calls:

Can I get my money back for the mug?
  Votes: {'refund': 3}
  Label: refund
The lamp came a week late and the shade is bent.
  Votes: {'shipping': 1, 'damaged': 2}
  Label: damaged
My teapot came late and cracked, and I want a refund.
  Votes: {'shipping': 1, 'damaged': 1, 'refund': 1}
  Label: human
Calls: 9 Tokens: 345
Where is my parcel? It's been two weeks.
  Votes: {'shipping': 2, 'refund': 1}
  Label: shipping
Model calls: 12

Your task: finish vote(ticket). Call classify 3 times and count each label in counts. After the Votes print, return the label with at least 2 votes, or FALLBACK when there is none.


Assignment
Follow the Coding Tutorial to add the 3 calls, the counting and the majority check to vote.


Hint
counts.get(label, 0) + 1 counts a label even the first time it appears. Put return FALLBACK after the loop over counts, not inside it: it runs only when no label was returned.


STEP 1 OF 7
    Ctrl + Enter to check

    Aiq Majority Vote in Python

    Keep going with Python1,600+ step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free