Overview

1 Starting a fabulous adventure

This opening chapter sets the tone for a pragmatic, curiosity-driven tour of data structures and algorithms, asking what makes certain ideas feel “fabulous.” The answers include solving real problems off the beaten path, embracing counterintuitive wins like immutability for undo/redo, recognizing patterns that transfer across domains (for example, backtracking), and writing code that speaks the language of the problem rather than its mechanisms. It also celebrates delightful “magic tricks” in design, pushing language features to their limits, reframing problems for dramatic gains, connecting practice with theory, and—above all—keeping programming fun.

The chapter then clarifies foundations: a data structure organizes information; an algorithm is a finite procedure that may even involve randomness; and complexity relates input size to resource cost, commonly expressed with big O notation. It emphasizes that costs include more than time (e.g., memory) and that best-, average-, and worst-case behaviors all matter—plus that asymptotically “better” algorithms are not always better for typical workloads. A warm-up in C# builds an immutable linked list, revealing subtle design and performance tradeoffs: constant-time pushes, a sentinel-based Empty choice, record-generated value equality that can recurse deeply, a naive ToString that’s accidentally quadratic, and a straightforward O(n) reverse that preserves the original list. The lesson is that small, innocuous choices can hide significant costs.

Finally, the chapter previews the journey ahead: familiar structures (stacks, queues, trees) explored with twists such as persistence, memoization, compression, elegant reorderings, and even a surprisingly fast reversal technique. It foreshadows tool-building algorithms from real developer tooling—backtracking for graph coloring and pretty printing, and tree (anti-)unification—and culminates in a practical toolkit for probability, randomness, and Bayesian inference, with brief theoretical interludes that connect everyday types to deeper patterns. Although the implementations use C#, the concepts generalize broadly, and the next chapter dives further into immutable lists and richer structures built from them.

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 Chapter 1 set out to cover?It introduces what makes certain algorithms and data structures “fabulous,” defines data structures, algorithms, and complexity, and builds a simple immutable linked list in C# to explore performance and a linear-time reversal.
What makes an algorithm or data structure “fabulous” in this book’s sense?
  • Solves a real problem in an unconventional way
  • Produces counterintuitive benefits (e.g., immutability aiding undo/redo)
  • Applies across diverse domains (e.g., backtracking in trees, maps, Sudoku, pretty printing)
  • Emphasizes problem meaning over implementation mechanics
  • Feels like a “magic trick” that reveals deeper ideas
  • Leverages language features to the fullest
  • Reframes problems to unlock dramatic gains
  • Connects practical code to theory
  • Makes programming fun
How does the chapter define a data structure and an algorithm?A data structure organizes information in a structured way. An algorithm is a finite sequence of steps that solves a problem; it may be nondeterministic, need not run on a computer, and can be conceptually simple even if it performs many steps.
What is algorithmic complexity and how is it measured?Complexity relates problem size to the cost of solving it, measured in resources like time or memory. We typically describe it with Big-O notation and consider best-, average-, and worst-case behavior; the relevant resource (time, memory, etc.) should be stated.
What are common Big-O classes and example scenarios?
  • O(1): Constant work, e.g., get the first element of an array
  • O(log n): Grows with doublings, e.g., minimal identifier length growth
  • O(n): Linear scan, e.g., summing n integers
  • O(n log n): Typical comparison sorting of n items
  • O(n²): Naive repeated string concatenation of n parts
  • O(2ⁿ) or O(n!): Explodes rapidly, e.g., listing all orderings of n items
What does “problem size” mean in practice?Often it’s the number of elements processed, but it can differ; for trees, complexity might be stated in terms of height rather than node count. Authors should make the size measure explicit.
Why isn’t the asymptotically better algorithm always the right choice?Real workloads are often small or structured. A naive method with a bad worst case can outperform an asymptotically superior one on typical inputs (e.g., short string searches), so practical behavior matters as much as Big-O.
How is the immutable linked list represented in the chapter’s example?As a C# record LinkedList(Value, Tail) with a shared Empty sentinel. Push creates a new node in O(1). Using a real Empty object (not null) makes method calls safe but requires care to avoid invalid states and subtle performance costs.
What performance pitfalls were found in the initial list implementation?
  • IsEmpty used record equality, making it pricier than a simple Tail == null check
  • Record-generated value equality is recursive and O(k) on shared prefixes; it can overflow the stack on long lists
  • ToString concatenated strings naively, causing accidental O(n²) time and memory; a StringBuilder-based approach should be used
How do you reverse an immutable linked list, and what is its complexity?Iterate the original list, pushing each head onto a new list; the original remains unchanged. The reversal runs in O(n) time and uses O(n) additional 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