Skip to main content

Data Shape Controls What Feels Fast

What This Concept Is

An algorithm's performance depends not only on the steps it takes but also on how the data is organized. Arrays, linked lists, and hash tables each make different operations feel cheap or expensive.

Why It Matters Here

Many "algorithm problems" are really data-shape problems. If you store information in the wrong form, even reasonable code feels slow. If you store it in the right form, the algorithm becomes much simpler.

Concrete Example

Suppose you are building a small system with three needs:

  • read the fifth item instantly
  • insert new items in the middle often
  • look up a price by product name without scanning a whole list

Those are not the same job.

NeedBest first fitWhy
Jump to item #5 quicklyArrayDirect index access is easy
Insert in the middle repeatedlyLinked listRe-linking can be cheaper than shifting many items
Find apple -> 0.67 by keyHash tableKey lookup is usually constant-time on average

Common Confusion / Misconception

There is no universally best data structure. Arrays are not "old and bad," linked lists are not "always better for inserts," and hash tables are not truly magical. Hash tables are powerful because they give very fast average lookup by key, but poor hashing or too many collisions can damage that performance.

How To Use It

Before choosing a structure, name the operation you care about most:

  1. random reads by position
  2. frequent inserts or deletes in the middle
  3. lookup by key

Then choose the structure that makes that operation cheap enough.

Also ask what tradeoff you are accepting. Faster lookup often comes with more structure, more memory use, or loss of ordering guarantees.

Check Yourself

  1. Why are arrays good at random access?
  2. Why can linked lists be awkward if you keep jumping to the tenth item?
  3. Why can a bad hash function make a hash table behave badly?

Mini Drill or Application

Choose the best first-fit data structure for each scenario and justify it in one sentence:

  1. a daily expense log you mostly append to and review later
  2. a browser history where you mostly move step by step through entries
  3. a username-to-profile lookup
  4. a ranked list where you frequently insert people into the middle

If two structures seem plausible, say what tradeoff would decide it.

Read This Only If Stuck