Overview

1 Starting a fabulous adventure

This opening chapter introduces the book’s idea of “fabulous” algorithms and data structures: techniques that solve real problems while also surprising, delighting, or changing how a programmer thinks. The author emphasizes approaches that go beyond standard classroom material, such as immutable structures, backtracking across different domains, algorithms that feel like magic tricks, and solutions that use language features or theoretical ideas in powerful ways. The chapter frames programming as both practical problem solving and creative exploration.

The chapter then defines the core concepts of data structures, algorithms, and complexity. A data structure organizes information, an algorithm is a finite procedure for solving a problem, and complexity describes how the cost of solving a problem grows as the problem size increases. It reviews common Big O categories, from constant and logarithmic time through linear, quasilinear, quadratic, exponential, and factorial growth. It also stresses that performance analysis should consider not only time, but also memory, resource use, and best-, average-, and worst-case behavior, while remembering that asymptotically “better” algorithms are not always better for typical real-world inputs.

As a warmup, the chapter builds and analyzes an immutable linked list in C#. This example shows how even simple code can hide design and performance issues, such as unsafe object states, generated equality behavior, possible stack overflow from recursive comparisons, and accidentally quadratic string concatenation. The chapter then demonstrates reversing an immutable linked list by walking through the original list and pushing each value onto a new list, producing an unchanged original and a reversed copy in linear time and memory. It closes by previewing later topics, including persistent structures, memoization, backtracking, unification, probability tools, randomness, Bayesian inference, and theoretical connections to everyday data types.

A graph comparing the growth curves for logarithmic, linear, quasilinear, quadratic and exponential growth. Logarithmic is the shallowest curve, growing very slowly, and exponential is the fastest.

Summary

  • There are lots of books about standard data structures such as lists, trees and hash tables, and the algorithms for sorting and searching them. And there are lots of implementations of them already in class libraries. In this book we’ll look at the less well-known, more off-the-beaten-path data structures and algorithms that I had to learn about during my career. I’ve chosen the topics I found the most fabulous: the ones that are counterintuitive, or seemingly magical, or that push boundaries of languages.
  • I had to learn about almost all the topics in this book when I needed a new tool in my toolbox to solve a job-related problem. I hope this book saves you from having to read all the abstruse papers that I had to digest if you too have such a problem to solve.
  • But more important to me is that all these fabulous adventures gave me a deeper appreciation for what computer programmers are capable of. They changed how I think about the craft of programming. And I had a lot of fun along the way! I hope you do too.
  • Asymptotic complexity gives a measurement of how a solution scales to the size of the problem you’re throwing at it. We generally want algorithms to be better than quadratic if we want them to scale to large problems.
  • Complexity analysis can be subtle. It’s important to remember that time is not the only resource users care about, and that they might also have opinions on whether avoiding a bad worst-case scenario is more or less important than achieving an excellent average performance.
  • If problems are always small, then asymptotic complexity matters less.
  • Reversing a linked list is straightforward if you have an immutable list! We’ll look at other kinds of immutable lists next.

FAQ

What does the chapter mean by “fabulous” data structures and algorithms?“Fabulous” data structures and algorithms are those that do more than solve standard textbook problems. They may solve real-world problems in unusual ways, produce counterintuitive results, apply across many domains, emphasize meaning over low-level mechanisms, resemble a “magic trick,” take full advantage of language features, challenge the usual framing of a problem, connect with theory, or simply make programming fun.
What is a data structure?A data structure is a way to store information in an organized and structured manner. In the chapter’s C# examples, data structures are created using arrays, classes, structs, records, and interfaces.
What is an algorithm?An algorithm is a finite sequence of steps that, when executed, solves a problem. Algorithms do not have to be deterministic, do not have to be run by a computer, and do not have to be conceptually complicated even if they perform many steps.
What is algorithmic complexity?Algorithmic complexity describes the relationship between the size of a problem and the cost of solving a problem of that size. Cost is often measured in time, but it can also involve memory or other system resources.
What is big O notation used for?Big O notation is the standard way to describe how an algorithm’s cost grows as the problem size grows. Common examples include O(1) constant time, O(log n) logarithmic time, O(n) linear time, O(n log n) quasilinear time, O(n2) quadratic time, and exponential or factorial growth such as O(2n) or O(n!).
Why is an algorithm with better asymptotic complexity not always the best choice?Better asymptotic performance matters most when problems become large. For small or typical inputs, a theoretically “worse” algorithm may be faster in practice because it has lower overhead or performs well on the common cases users actually encounter.
How is the immutable linked list defined in this chapter?The chapter defines a linked list recursively: it is either empty, or it is a value followed by a tail linked list. The example implementation uses a C# record with a Value and a Tail, plus an Empty list, a Push method, and an IsEmpty property.
Why is using null for the empty linked list considered problematic?In an object-oriented language, representing the empty list as null means the empty list is not a valid object that can receive method calls. The chapter instead represents the empty list as an object, though the initial design is still imperfect because it can allow invalid states, such as a non-empty list with an invalid Tail.
What hidden performance problems appear in the initial linked list implementation?The chapter points out several issues: record-generated equality can be more expensive than expected, equality on recursively defined lists can overflow the call stack for long lists, and the naive ToString implementation is accidentally quadratic because repeated string concatenation copies previous strings and creates garbage.
How does the chapter reverse an immutable linked list, and what is its complexity?The Reverse method starts with an empty list, walks through the original list one item at a time, and pushes each value onto the result list. Because the list is immutable, the original list remains unchanged. The method is O(n) in both time and extra memory.

pro $24.99 per month

  • access to all Manning books, MEAPs, liveVideos, liveProjects, and audiobooks!
  • choose one free eBook per month to keep
  • exclusive 50% discount on all purchases
  • renews monthly, pause or cancel renewal anytime

lite $19.99 per month

  • access to all Manning books, including MEAPs!

team

5, 10 or 20 seats+ for your team - learn more


choose your plan

team

monthly
annual
$49.99
$499.99
only $41.67 per month
  • five seats for your team
  • access to all Manning books, MEAPs, liveVideos, liveProjects, and audiobooks!
  • choose another free product every time you renew
  • choose twelve free products per year
  • exclusive 50% discount on all purchases
  • renews monthly, pause or cancel renewal anytime
  • renews annually, pause or cancel renewal anytime
  • Fabulous Adventures in Data Structures and Algorithms ebook for free
choose your plan

team

monthly
annual
$49.99
$499.99
only $41.67 per month
  • five seats for your team
  • access to all Manning books, MEAPs, liveVideos, liveProjects, and audiobooks!
  • choose another free product every time you renew
  • choose twelve free products per year
  • exclusive 50% discount on all purchases
  • renews monthly, pause or cancel renewal anytime
  • renews annually, pause or cancel renewal anytime
  • Fabulous Adventures in Data Structures and Algorithms ebook for free