LeetCode and Data Structures
Hi! 👋 Currently, I’m preparing for my internship next year, and these days I’m working hard on LeetCode problems.
At first, I thought it was enough to just:
- Try solving a problem
- Check the solutions
- Understand the idea
- Try it again
Well… it works. But honestly, you’ll be faster (and way less frustrated) if you know some common patterns.
In the next few posts, I’d like to write a series about the most popular techniques that can help you solve about 60–70% of problems on LeetCode (easy → medium level).
In this post, I want to introduce some basic data structures. Once you figure out how these work, you’ll be able to understand most of the others much more easily.
What’s a data structure?
A data structure is just a way to efficiently store data. That’s it.
But it’s really important to choose the right one for the right task. Otherwise, you’ll waste time (and get frustrated) trying to figure out which operations make sense for the structure you picked — or why your time complexity suddenly jumps to O(n^2)
just because you tried to add a few characters to a string.
1. Array
String[] myFamily = {"Rick", "Morty", "Summer", "Beth", "Jerry"};
Array is a linear, continuous data structure used for storing data.
Most common example: think about a row of lockers with numbers (indices). You open one and put your data inside.
Some features:
- Strings are arrays of characters.
- Access time:
O(1)
→ you know your locker, just open it and get your value. mmm, nice, love it. - Insert/Delete at the end/start:
O(1)
- Insert/Delete in the middle:
O(n)
(notn^2
, so not terrible — just shift N elements).
Where to use:
- Traversing a structure in order
- Accessing specific indices
- Comparing elements from both ends
- Sliding window, prefix sum, etc.
2. String
String heroName = "Morty";
Well, strictly speaking, string is not a data structure, but it deserves to be mentioned.
A string is basically an array of characters.
Main things to remember:
- In memory, a string really is an array of characters.
- Immutable in most languages: for example, when you do
"dog" + "s"
, it doesn’t change the first string — it creates a totally new one.
When to use it:
- Checking for anagrams
- Returning a substring that matches a pattern
- Finding the longest substring without repeating characters
- String problems are not for brute-force solutions — you’ll need smart techniques.
3. Set
HashSet<Integer> mySet = new HashSet<>(); mySet.add(0); mySet.add(1); mySet.add(6); mySet.add(5);
Set is a collection of unique variables. That’s it.
Some features:
- Access time:
O(1)
(usually) - Stores only unique elements
Where to use it:
- When you need uniqueness
- When you want to check for existence
- When you need fast membership checks (
Array
needsO(n)
,Set
does it inO(1)
)
4. Map (Dictionary)
HashMap<String, String> contacts = new HashMap<>(); contacts.put("me", "123-123-123"); contacts.put("dad", "000-000-000");
A map is a collection of key–value pairs. Like a phone book: name → phone number
.
Features:
- Access:
O(1)
(usually); worst case isO(n)
but rare - Keys must be unique
Where to use it:
- Counting frequencies
- Caching
- Mapping indices
Summary
That’s it for the basic data structures.
All the others (trees, graphs, linked lists, etc.) are like upgraded versions of these four. If you nail these, you’ll pick up the rest much more easily.
In my next posts, I’ll cover common patterns and real LeetCode problems.
I hope you liked this post — see you later! 👋
Top comments (2)
This feels like the “starter pack” every beginner wishes they had. Arrays are basically lockers, strings are lockers with letters, sets are lockers where the guard won’t let duplicates in, and maps are just that one friend who remembers everyone’s phone number. Looking forward to the sequel of this sitcom.
Thanks for your comment! I agree with you — it really is the basics starter pack.
Personally, in the beginning I thought there were so many data structures that it would take forever to learn them all. But in the end, you just need a solid grasp of the basics — and you’ll be good 😄