|
Databases

Data structures you will actually use at work

You will not write a red black tree. You will count things with a dictionary and remove duplicates with a set. Here is the small set that fixes most slow loops.

Data structures you will actually use at work

Nobody has ever asked me to implement a red black tree at work. Not once, in years of building software for clients. What I do every week is count things with a dictionary, remove duplicates with a set, and keep a list in order. That is most of the job.

This matters because data structures are taught as a memory test. Big O notation, ten structures, an exam. Then you get to a real codebase and the question you face is: why is this loop slow? Usually the answer is you searched a list when you should have used a dictionary.

The list is the default, and that is fine

A list (array, in most languages) holds items in order. You append, you loop, you index. For 90 percent of what you write, a list is correct and you should not feel bad about it.

The trap is searching. if name in customers where customers is a list of 50,000 names checks each one until it finds a match. That is fine once. Inside a loop over another 50,000 rows, it is 2.5 billion comparisons and your script takes an hour.

The moment you catch yourself searching a list inside a loop, stop and change the structure.

The dictionary is the workhorse

A dictionary (hash map, object, whatever your language calls it) stores key to value pairs and finds a key in roughly constant time, no matter how many entries there are. That one property fixes most slow code.

Say you have a list of orders and a list of customers, and you need to attach each customer's name to their orders. The beginner version loops orders, and for each order loops customers to find the match. With 10,000 of each, that is 100 million steps.

The fix takes four lines. Build a dictionary of customers keyed by id. Then loop orders once and look up the name. Two loops, 20,000 steps. On my laptop that is the difference between 40 seconds and 0.02 seconds.

Counting is the other use. Instead of a counter variable per category, use a dictionary and increment counts[key]. Word frequency, sales per product, orders per day. Same pattern every time.

I spent a month memorising the complexity table for interviews and then wrote production code that searched lists inside loops for two years. The interview knowledge did not transfer. The habit of asking "how many times does this run?" did.

Now, before I write a nested loop, I ask one question: is the inner collection being searched for a specific item? If yes, it should be a dictionary or a set, not a list. That single check has saved me more time than any algorithm I memorised.

Sets for duplicates and membership

A set is a collection with no duplicates and fast membership checks. Two jobs it does better than anything else.

  • Removing duplicates from a list: convert to a set, convert back. One line in Python.
  • Checking whether an item is allowed: if email in blocked_emails is instant on a set and slow on a list.

I use sets constantly for deduplication when importing messy data. A customer list from three branches will have the same person three times with different spacing. Normalise the phone number, put it in a set, and you get the unique count in one pass.

The catch: sets are unordered and they drop duplicates silently. If order matters or duplicates are meaningful, use a list.

Stacks and queues when order matters

A stack is last in, first out. A queue is first in, first out. You will meet both in real work more than you expect.

The undo button is a stack. Every action pushes onto it, undo pops the last one. The browser back button works the same way. A queue is a job list: orders to process, emails to send, files to upload. First one in gets handled first.

In practice you rarely build these by hand. You use a list with append and pop for a stack, and a library queue for jobs. But knowing which order you need stops you from picking the wrong one and wondering why the newest order ships first.

The tree you will meet

You will not write a tree. You will use one and not notice. A database index is a tree. A file system is a tree. The folder structure of your project is a tree. Every time you click through nested categories on an ecommerce site, you are walking one.

The useful skill is recognising when your data is hierarchical: categories with subcategories, comments with replies, staff with managers. In a flat table that becomes a parent_id column, and suddenly "show this category and everything under it" is a real query instead of a mess of loops.

A small exercise

Take a CSV of 5,000 orders with a customer id column. Load it into a list. Write the slow version first: loop orders, loop a customer list to find the match, print the first 10 results. Time it. Then rewrite it with a dictionary and time it again. Keep both numbers.

If you have never written that loop, start with Python for absolute beginners. When you want to see structures inside a real app, read how to build your first CRUD app. The software development course connects the two, because a database is a data structure with a query language on top.

Dave Partner

Dave teaches digital skills at Braintem and has helped thousands of Nigerians move from watching tutorials to charging for real work. He writes about the parts nobody puts in the course trailer.