Ready to begin
No records have been processed. Start with Next step.
Keeping one maximum needs one remembered value. Keeping three needs three ordered places: First ≥ Second ≥ Third. When a new maximum arrives, save the old values before overwriting them.


| New value v | What to change |
|---|---|
| v > First | Third = Second; Second = First; First = v |
| First > v > Second | Third = Second; Second = v |
| Second > v > Third | Third = v |
| v below Third | No update |
Use mutually exclusive cases, such as if / else if, or explicit disjoint conditions. Independent tests that reuse a just-updated value can accidentally process one card twice. The order of assignments matters: writing First = v too early loses the old First.
Read the current record, check the explanation, and watch the variables. Use Back to revisit any step.
Ready
No records have been processed. Start with Next step.
Start at the first step. Watch the current row and the values we remember.
| Field | Top three distinct mark levels | Top three student scores, counting ties |
|---|---|---|
| Mathematics | 97, 93, 89 | 97, 97, 93 |
| Physics | 92, 91, 88 | 92, 92, 92 |
| Chemistry | 97, 93, 92 | 97, 93, 92 |
The strict inequalities in the lecture’s TopThreeMarks code keep distinct score levels: repeated 97 does not create another mark level. A ranking of three students instead counts two different students with 97 separately. The walkthrough uses distinct levels and lets you change the field parameter.
Procedure TopThreeMarks(Rows, Field)
First = -1; Second = -1; Third = -1
for each row X in Rows {
v = X[Field]
if (v > First) {
Third = Second
Second = First
First = v
} else if (First > v and v > Second) {
Third = Second
Second = v
} else if (Second > v and v > Third) {
Third = v
}
}
if (Third == -1) { report "Fewer than 3 distinct values" }
else { return Third }
End ProcedureThe source initializes marks to 0 because these marks are nonnegative. Here −1 marks an empty slot explicitly, so a valid zero is not confused with a filled slot. This bound is for marks, not arbitrary data. If fewer than three different values exist, do not return a placeholder as a genuine cutoff.
Current values are 90,80,70. What happens when 95 arrives?
Third becomes 80, Second 90, First 95. Save lower slots first.
Why not write First=95 before Second=First?
Second would receive 95 instead of the old First 90, losing information.
Do repeated 97 s create another distinct score level?
No. They are equal to the existing level.
What is the third distinct Mathematics level here?
89. The third student score counting ties is 93.
What should happen with only two distinct values?
Report that no third distinct cutoff exists; do not treat an empty placeholder as a mark.