Create your free BvLogic ID to continue this course and save your progress.
Data Structures and Algorithms 5 lessons
  1. 1 Why this subject exists
  2. 2 Arrays, lists and hash tables sign in to open
  3. 3 Stacks, queues and trees sign in to open
  4. 4 Sorting and searching sign in to open
  5. 5 Recursion and dynamic programming sign in to open
Course overview
Data: video · change

This lesson: 122KB

Data Structures and Algorithms · Lesson 1 of 5

Why this subject exists

Understand cost before you memorise structures.

Every data structure is a trade. You are choosing what to make fast at the expense of something else, and the whole subject follows from that.

Big O notation describes how the work grows as the data grows. It deliberately ignores constants, because a difference of two times stops mattering when the difference between approaches is ten thousand times.

Five categories cover almost everything you will meet. Constant time, where the work does not grow at all, such as reading an array by index. Logarithmic, where doubling the data adds one step, which is what binary search gives you. Linear, where the work matches the data, such as scanning a list. Linear times logarithmic, which is what good sorting costs. And quadratic, where doubling the data quadruples the work, which is what a loop inside a loop gives you.

The practical meaning is worth stating plainly. With a million items, a linear scan takes a million steps and a nested loop takes a million million. The first finishes now, the second does not finish today. That is the difference between a feature and an outage, and it is why interviewers ask.

Space matters too. A hash table makes lookups fast by using memory to do it. On a server that is usually a good trade. On a phone with 2GB of RAM it may not be.

One caution. For small inputs, the simple approach usually wins, because constants and readability dominate. Optimise when you have measured, not when you have guessed.

Lab — try it yourself

Write a nested loop that finds duplicates in a list, then rewrite it using a set. Time both on ten thousand items and compare.

Check what you learned

Create your free BvLogic ID to take the quiz and record your score.

Create your BvLogic ID
Continue to lesson 2 Up next 2. Arrays, lists and hash tables