Keep the newest digits

Update a short ordered group as new digits arrive, then report that group when the stream stops.

Make room for each new digit

Before 4 arrives258After 4 arrives584
Drop the oldest digit. Keep the remaining order, then add the new digit.

Before the stream starts, read how many digits to report. Keep only that many of the most recent digits, in order. When another arrives, drop the oldest and add the new one at the end.

More

Suppose you are keeping three: 2, 5, 8. When 4 arrives, let 2 go and keep 5, 8, 4. The figures are snapshots to explain this update; the practice shows just one digit at a time.

Lesson help

Drop the oldest, add the newest, and preserve the order of the group you keep.

About this lesson

Try the method in a practice when you are ready.

Report

Make room for each new digit

Before the stream starts, read how many digits to report. Keep only that many of the most recent digits, in order. When another arrives, drop the oldest and add the new one at the end.

Suppose you are keeping three: 2, 5, 8. When 4 arrives, let 2 go and keep 5, 8, 4. The figures are snapshots to explain this update; the practice shows just one digit at a time.

Before 4 arrives258After 4 arrives584
Drop the oldest digit. Keep the remaining order, then add the new digit.

Stop with the group you are holding

This short example stream is 8, 3, 6, 1, 9, then it stops. If asked for the last three, type 619. Those digits stay in the order you saw them, not newest first.

The practice does not reveal the stream length or count down to the end. Keep updating throughout. Lower levels ask for the last two digits, then later levels ask for three or four. Recall is untimed.

Stopped example stream83619
Last three: 6, 1, 9. The first two digits are no longer needed.

Keep the last two, in order

You were asked to report the last two digits. This example stream has now stopped. Which answer keeps those two in their original order?

Stopped example stream84726
Report the last two digits.

Try it

The stream ends 8, 4, 7, 2, 6. What are the last two digits, in order?

  1. 62
  2. 26
Show the answer

26

Yes. Keep 2 and then 6. Report the newest two digits in the order they arrived.

Enable JavaScript to answer interactively and save your place.

Practice Running span

Sign in free

Practice without limits and keep your progress.