Custom Data Structures: Introduction
Let’s go over the Custom Data Structures pattern, its real-world applications and some problems we can solve with it.
We'll cover the following
Overview#
By now, you’re probably aware of a lot of data structures. Some popular data structures include array, linked list, stack, and queue. Many problems can be solved using these existing data structures. However, there might be instances where you need a more specialized data structure to solve the problem. This is where the custom data structure pattern comes into play. A custom data structure does not have to be something completely novel – it can be a modified version of an existing data structure. For example, you can modify the tree structure to also include pointers to parents or even add some other data structure like an array for every node.
Using custom data structures makes it easier and more efficient to solve problems that would otherwise be difficult with the existing data structures.
The illustration below shows some commonly used data structures that can be used to make a custom data structure:
1 of 4
2 of 4
3 of 4
4 of 4
Example#
The following example illustrates a problem that can be solved with this approach:
1 of 2
2 of 2
Does my problem match this pattern?#
- Yes, if either of these conditions is fulfilled:
- The problem requires customizing an existing data structure, that is, adding a feature to it or modifying an existing feature. Examples include min stack and maximum frequency stack.
- The problem requires combining one or more data structures to solve the problem efficiently. An example would be implementing a Least Recently Used (LRU) cache.
- No, if existing data structures may be used to efficiently solve the problem.
Real-world problems#
Many problems in the real world share the custom data structure pattern. Let’s look at some examples.
-
Keeping various states of games: By modifying/combining the standard data structures, maintain the state of the players, levels, and other relevant game details efficiently.
-
Used in search engines: Search engines use custom data structures such as customized trees and arrays to quickly search and display data.
-
Data in tabular format: Data can be stored in tabular format and accessing data can be made much more efficient by tweaking existing data structures. For example, say you have data in JSON format, which contains nested arrays. In order to access the nested arrays, the entire JSON string needs to be processed to find it. We can design a custom data structure that enables efficient access and updating of these nested arrays.
Strategy time!#
Match the problems that can be solved using the custom data structures pattern.
Note: Select a problem in the left-hand column by clicking it, and then click one of the two options in the right-hand column.
Find the largest element in an array.
Custom Data Structures
Implement arbitrary state persistence of multiple workers nodes to improve the fault tolerance of a distributed processing system.
Some other pattern
Enhance a stack to enable popping the highest value in O(1).
Detect a cycle in a linked list.
Solution: Word Search II
Snapshot Array