Learn to choose a data structure or algorithm, trace its behavior on a small input, and explain its time and space costs.
A package scanner records the IDs [42, 17, 42, 31] in arrival order. Someone asks, “When did package 42 first appear?” A scan from the left answers the question at index 0. If thousands of people will ask about the same log, it may be worth recording the first index of each ID ahead of time. The input stayed the same. The cost of answering the next question changed.
This course starts with decisions like that one. You’ll need to be comfortable reading variables, conditions, loops, and functions in JavaScript or Python. You can work through the course in either language.
| Chapter | Question you’ll learn to answer |
|---|---|
| Big O notation | If the input doubles, what happens to the work and the extra memory? |
| Built-in data structures | Does this problem need a position, a count, or a membership test? |
| Algorithm patterns | Which fact can you keep as you move through an array or string? |
| Linked lists, stacks, and queues | Which item must be reachable next, and which links or endpoints make that possible? |
| Recursion | What smaller input can solve part of the problem, and when can a call return immediately? |
| Sorting | Which order do you need, and what does it cost to establish that order? |
| Searching | What property lets a search rule out a whole range of candidates? |
Trace one small input before running the code. At each step, write down only the state that the algorithm carries forward. In Two Sum, for example, that means the current value and the earlier values you can look up. In a practice lesson, try the editor before reading the solution. Then test an input that would expose a mistake, such as a pair made from two equal values at different indices.
Return to the scanner log after the searching chapter. The log is fixed, and you can spend extra memory if it helps. What would you do for one first-position query? What would you prepare for many queries? If someone handed you only a sorted copy of the IDs, could you still recover the first arrival position?
For one query, scan from the left and stop at the first match. This takes up to O(n) time and O(1) extra space. For many queries, scan once and store each ID’s index the first time you see it. The map for this log stores 42 → 0, 17 → 1, and 31 → 3. Overwriting the entry for the second 42 would give you its last position.
With ordinary hash-map assumptions, building the map takes O(n) expected time and answering a query takes O(1) expected time. Space grows with the number of distinct IDs. A sorted copy of the IDs alone loses the arrival positions. To use a sorted index for this question, you would also need to keep the original positions.