A girl and a robot thinking together

Think together · First, a short story

The girl needs to process many requests to find out who registered first. The robot considers the operations instead of picking an algorithm just because it sounds impressive.

Lay out the process

Follow the numbered steps, then explain them in your own words.
  1. Identify the operationTake people in registration order
  2. Preserve the orderChoose a queue
  3. Account for scaleEstimate requests and people
  4. Verify the ruleEarlier arrivals are processed first

Data structures and algorithms should work together to solve the task.

Look at how the data changes, what you query most often, and how large it gets. The structure stores the relationships you need; the algorithm uses them to perform the operations.

01Choose the algorithm or the data structure first?

Usually, consider them together.

How data is stored affects how an algorithm can process it, while the algorithm's needs also determine which structure is useful.

02Start with the data relationships

Sequential data

Array / vector

Last in, first out

stack

First in, first out

queue

General connections

graph

03Then look at the common operations

What does the problem do most often?

  • Access by index?
  • Find the minimum?
  • Insert and remove items repeatedly?
  • Find a shortest path?
  • Answer many range queries?

04Then consider the input size

The same logic can have very different costs:

n ≤ 100

O(n²) may be perfectly manageable.

n ≤ 200000

O(n²) is usually too slow.

05Do keywords directly determine the algorithm?

No. Do not match them mechanically.

06How can you build a selection process?

Build a model→List feasible algorithms→Filter by the constraints

Then compare implementation effort, memory use, and risks to correctness.

What have you learned?

  • Design the data structure and algorithm together.
  • Start with data relationships and common operations.
  • Input size determines which complexity is feasible.
  • Do not choose an algorithm by mechanically matching keywords.
  • A correct model and complexity estimate are central to choosing an approach.

Next: What are boundary cases?

Your turn to try

If the task changes to “undo the most recent registration,” is a queue still the most direct choice?

Ready? Open the explanation

A stack is a better fit when the most recent item should be undone first. When the task rules change, the useful operation order changes too.