Data structures and algorithms for firmware: what to learn, in what order
Most data structures and algorithms (DSA) material is written for web or competitive programming: unlimited memory, garbage collection, and "fast" meaning the best average case. Firmware changes the rules. Memory is measured in kilobytes, malloc is often forbidden, and a function that is fast on average but occasionally takes 50 times longer can miss a deadline. So you need the same ideas, learned with different questions.
The questions firmware asks
For any structure, ask:
- How much RAM does it cost, exactly? Count bytes, including padding and pointers.
- What is the worst-case time, not the average? Deterministic latency often matters more than throughput.
- Can it live in a fixed-size static buffer with no dynamic allocation?
- Is it safe to touch from an interrupt, and who writes what?
- What happens when it is full?
A sensible order
- Arrays, strings and pointers. Everything else is built on them. Be fluent with pointer arithmetic, bounds and
const. - Bit manipulation. Registers, masks and bitmaps. See bit tricks for firmware.
- Ring buffers and queues. The workhorse between interrupts and main code. See the ring buffer guide.
- Linked lists with a static node pool. Cheap insert and remove with no
malloc, at the price of pointer overhead and poor cache behavior. - Sorted arrays, binary search and hash tables. Lookup structures. Often a sorted array beats a hash table in firmware because it is smaller and has a simple worst case.
- State machines. Not a classic DSA chapter, but the most used design tool on a microcontroller. See the debounce example.
- Stacks, heaps and priority queues. Scheduling, timers and event ordering.
- Trees and graphs. Routing tables, dependency ordering, parsing. Less common but they appear in larger systems and in interviews.
- Fixed-point math, CRC and checksums. Algorithms that replace floating point and protect data. See fixed-point without an FPU.
Learning this for work or an interview? Lessons 1 and 2 of DSA for Firmware are free, with labs and C exercises. Start Lesson 1
Choosing a structure under constraints
Suppose you track 64 connected devices by ID, with 90% lookups and occasional inserts or removals, deterministic latency required, no dynamic allocation, and an 8 KB RAM budget. A sorted array with binary search fits well: 64 entries of a few bytes each, lookup in at most 7 comparisons, insert by shifting at most 63 entries, and no pointers. A hash table needs spare capacity and collision handling, and a binary tree needs pointers and balancing. The code below confirms the lookup bound:
#include <stdint.h>
#include <stdio.h>
#define N 64u
typedef struct { uint16_t id; uint8_t state; } dev_t;
static unsigned probes;
static int find(const dev_t *a, unsigned n, uint16_t id)
{
unsigned lo = 0, hi = n; /* search in [lo, hi) */
while (lo < hi) {
unsigned mid = lo + (hi - lo) / 2u;
probes++;
if (a[mid].id == id) return (int)mid;
if (a[mid].id < id) lo = mid + 1u; else hi = mid;
}
return -1;
}
int main(void)
{
dev_t table[N];
for (unsigned i = 0; i < N; i++) { table[i].id = (uint16_t)(100u + 3u * i); table[i].state = 0; }
unsigned worst = 0;
for (unsigned i = 0; i < N; i++) { /* every present id */
probes = 0;
if (find(table, N, table[i].id) != (int)i) { puts("FAIL lookup"); return 1; }
if (probes > worst) worst = probes;
}
for (uint16_t id = 0; id < 400; id++) { /* absent ids, too */
if ((id >= 100 && (id - 100u) % 3u == 0 && id < 100u + 3u * N)) continue;
probes = 0;
if (find(table, N, id) != -1) { puts("FAIL false hit"); return 1; }
if (probes > worst) worst = probes;
}
printf("table: %u entries x %zu bytes = %zu bytes of RAM\n", N, sizeof(dev_t), sizeof table);
printf("worst-case probes over hits and misses: %u (log2(64)+1 = 7)\n", worst);
return worst <= 7 ? 0 : 1;
}
table: 64 entries x 4 bytes = 256 bytes of RAM worst-case probes over hits and misses: 7 (log2(64)+1 = 7)
Compiled with -Wall -Wextra -Werror and run under AddressSanitizer + UBSan.
The answer would change if the numbers changed. With thousands of entries and frequent inserts, you would reconsider. The skill is reading the constraints, not memorizing one "best" structure.
What you can skip at first
Red-black trees, tries, skip lists and most advanced graph algorithms are rarely needed on a microcontroller. Learn them later, once you can reason about RAM, worst case and interrupts for the basics.
If you are preparing for interviews
Embedded interviews mix C details, bit manipulation, data structures, concurrency and hardware reasoning. The companion piece firmware interview questions with answers covers the questions that come up most.
DSA for Firmware teaches it one real bug at a time.
- 28 lessons, each starting from a real firmware bug.
- 110 C exercises with tests, and a reference solution for every one.
- Lessons 1 and 2 are free. No sign-up.
Get the next article by email
One short email when a new article or free lesson is published. No spam. Unsubscribe at any time by replying to any email. See the privacy policy.