Data structures 101: How to choose the right data structure
Here's a 4-step process that will help you select the optimal data structure for any problem you encounter.
Once you understand some programming basics, like loops and conditionals, you're ready to learn data structures. As a way to store data, data structures open the door to cleaner and organized code (and with that, peace of mind).
A common mistake I see from beginners when starting to learn about data structures is that they focus more on the what than the why.
In other words, they over-index on learning the syntax of each data structure, without taking the time to understand the characteristics that make certain data structures better for certain scenarios. Having the proper syntax for a data structure won't help you much if you're using that data structure in the wrong situation!
Today, I'm not even discussing syntax. Instead, I'll share a process for how to choose the right data structure for any use case.
I'll cover:
What are data structures? (A simple example)
Understanding trade-offs
A 4-step process to choosing the right data structure
What are data structures? (A simple example)
Data structures are indispensable tools that help you organize and manage information in a computer program. You can think of them as storage tools that help you code more efficiently.
Let's start with an introduction to one of the simplest data structures: arrays.
An array is a row of items or "elements" in a certain order. The items can be any type: numbers, words, or even more complex things.
Imagine an egg carton that has one row and holds 4 eggs. Each slot in the carton holds one egg. This is equivalent to saying we have an array with 4 elements. The positions where items are stored in an array are referred to as "indices" (plural for index). The indices of an array start at 0, where the first element is stored.
Arrays are one of the simplest ways to organize and manage collections of items in coding. You'll find arrays in programs for nearly any application you can think of, like keeping track of game scores, listing names in a contact book, etc.
Voila! You now know your very first data structure. But really understanding a data structure is more complex than this.
Understanding trade-offs
Every data structure has its own way of storing things, which affects how you can work with that data. For example, arrays make it very efficient to retrieve data. However, they're impractical if you want to rearrange the order of your data often.
When you're coding, you can choose from a variety of data structures for a given problem. Choosing a suboptimal data structure can make life unnecessarily difficult for you. To make a smart choice about which structure to use, you need to understand each data structure's pros and cons.
Just like the array, different data structures will be better optimized for certain scenarios, such as:
Updating (or changing) data quickly
Retrieving data quickly
Processing data quickly
Navigating data quickly
There's no such thing as a perfect data structure that can be optimized for all scenarios. You'll have to choose a data structure that has downsides that you're willing to accept. Any compromises made in the process are what we call trade-offs. Ultimately, a strong problem-solver knows how to make wise trade-offs.
A 4-step process for choosing the right data structure
When making a choice, the needs of your problem will guide you to the appropriate data structure.
Here's a 4-step process that you can use to choose data structures. (We'll explore this process through a simple analogy of storing new shoes.)
Understand your problem & its constraints
E.g., Computer has limited memory
Real-world example: You need to store new dress shoes, but have limited closet space.
Evaluate requirements for data you're storing
E.g. Data must be easy to update
Real-world example: Shoes must be easy to access once in a while, but not cause clutter.
Identify data structures that meet key requirement(s)
Real-world example: Two storage options are boxes under the bed, or a hanging rack behind the door.
Select the data structure with the most acceptable trade-offs
Real-world example: Because the hanging door rack causes clutter, the shoes can be stored under the bed. The trade-off of under-bed storage is that it takes effort to pull it out, but this is acceptable since you don't need the shoes often.
Learning how to choose data structures is just a matter of learning when to make acceptable trade-offs. You're never just picking a structure based off its pros — you need to remember its cons, and assess which con is most tolerable for the given scenario at hand.
You'll have better judgment over time
Becoming a strong programmer requires you to make strategic choices when solving problems. This is true whether you're deciding on data structure, algorithm, or more complex matters like software or system design.
How do you make the "right" choice? This is something you'll perfect over time. You will make "wrong" or non-optimal choices as you're learning, and that's ok! When you do, you may still be able to solve your problem — just less efficiently. These "wrong choices" are some of the best learning opportunities, as long as you assess why your choice wasn't practical, and take that lesson with you for the future.
As a reminder, you can find what you need to get from your first line of code to your first day on the job with Educative's Learn to Code subscription, including 30+ hands-on courses and projects in the industry’s most popular programming languages.
Happy learning!
– Fahim