Scrivio Interview library
ScenarioAdvanced7 min read · updated 12 November 2026

Big-O says an algorithm is O(n log n) but your measured latency grows much faster as data grows from 1 GB to 20 GB in memory. What factors that big-O hides would you look at across the stack?

Big-O counts operations, but it assumes every operation costs the same, and on real hardware that stops being true once data outgrows the CPU caches. Going from 1 GB to 20 GB, I would look at cache and TLB misses from pointer-chasing object graphs, garbage collection cost that grows with the live heap, memory inflation from object headers, boxing and lost compressed pointers that pushes the process towards swap or container limits, and contention between threads. Profilers, allocation profiling and hardware counters show which one it is, and primitive or columnar layouts are often the fix.

Why this question is asked

Engineers trust big-O for good reason, so a measurement that contradicts it feels like a bug in the measurement. It usually is not. Big-O is a model with hidden assumptions, and at tens of gigabytes in a JVM almost all of them break. Interviewers at this level want to see that we can name what the model leaves out and find which factor is to blame instead of guessing. In this answer, we will see what big-O assumes, then go through the memory hierarchy, garbage collection, memory inflation, concurrency effects, and finally how to measure and what to change.

1What Big-O Assumes

O(n log n) says that the number of basic operations grows in proportion to n log n. Going from 1 GB to 20 GB multiplies n by 20, and log n grows only slightly, so we expect about 22 to 25 times more time. If we measure 100 or 200 times more, something other than the operation count has changed.

The model hides two things in its constant factor: how long each operation takes, and costs outside the algorithm, such as the garbage collector and the operating system. Both are roughly constant when data is small. Neither is constant when data grows from a size that fits in fast memory to a size that does not.

2The Memory Hierarchy: Cache Misses and Pointer Chasing

A modern CPU reads from its L1 cache in about 1 nanosecond, L2 in about 4, the shared L3 cache in 10 to 20, and main memory in roughly 80 to 120 nanoseconds. L3 caches are tens of megabytes. Both 1 GB and 20 GB are far beyond them, but at 1 GB a larger share of the hot data, such as the upper levels of a tree or the most-used objects, stays cached, and the address translation entries the CPU keeps in its TLB (translation lookaside buffer) cover more of it. At 20 GB, nearly every access to a random object is a full trip to main memory, plus a page-table walk when the TLB misses.

Java makes this worse through object graphs. A List<Order> is an array of pointers to Order objects scattered across the heap, each pointing to more objects. Traversing it is pointer chasing: each read depends on the previous one, so the CPU cannot fetch ahead. An O(n log n) sort over such a list may spend most of its time waiting for memory. So the same number of operations can take 10 times longer because each one became a cache miss.

Step throughWhere a read is served, and roughly how long it takes
  1. L1 cacheabout 1 ns, tens of KB per core
  2. L2 cacheabout 4 ns, about 1 to 2 MB per core
  3. L3 cache10 to 20 ns, tens of MB shared
  4. Main memory80 to 120 ns, more with a TLB miss
  5. Swap or a remote NUMA nodemicroseconds or worse

5 layers, from the top down. Step through them one at a time.

At 20 GB, most random object accesses fall to main memory; figures vary by CPU.

3Garbage Collection Grows With the Live Heap

The garbage collector's work depends on how much live data it has to find and how fast the application allocates. A concurrent collector such as G1, the default, marks live objects across the whole heap periodically; with 20 times more live objects, each marking cycle takes far longer and competes with the application for CPU. If the application allocates faster than the collector can keep up, G1 falls back to longer pauses, and in the worst case to a full collection that stops the application for seconds.

A bigger working set also means fewer free regions, so collections happen more often. The GC log (-Xlog:gc*) or a JFR recording tells us quickly whether pause times and GC CPU share grew as data grew. ZGC keeps pauses short even with large heaps, but its concurrent work still uses CPU in proportion to the live data.

4Memory Inflation: Headers, Boxing and Compressed Pointers

In Java, data rarely takes the space its raw values need. Every object has a header, 12 bytes with compressed class pointers (8 bytes with compact object headers, a product feature in Java 25 enabled with -XX:+UseCompactObjectHeaders; check your version), and objects are padded to 8-byte boundaries. A boxed Long takes 16 bytes to hold 8 bytes of value, and every collection entry adds a node object. A data set that is 20 GB of raw values can easily take 40 to 60 GB as objects.

There is also a sharp cliff. Compressed pointers, which store references in 4 bytes, only work when the heap is below about 32 GB. A service that grows its heap from 24 GB to 40 GB to fit more data loses them, every reference doubles to 8 bytes, and the same objects need 20 to 40 percent more memory. If the process then exceeds the machine's RAM it starts swapping, and if it runs in a container it hits the memory limit and gets killed. Each of these turns a predictable algorithm into an unpredictable service.

5Contention and Coherence Under Concurrency

Big-O for a single thread says nothing about threads sharing data. With more data, more threads often work on it at once, and two costs appear. Lock contention makes threads wait for each other. Cache coherence traffic makes cores invalidate each other's cache lines when they write to shared or neighbouring memory; false sharing is the case where two unrelated fields sit on the same 64-byte cache line and the cores fight over it. On a machine with several sockets, memory attached to the other socket (a remote NUMA node) is noticeably slower to reach. None of this is in the operation count.

6Measuring Which Factor It Is, Then Changing the Layout

We measure rather than guess. A CPU profile from async-profiler shows where time goes; its allocation mode shows who allocates; and on Linux, perf stat -e cache-misses,dTLB-load-misses (or async-profiler's hardware counter events) shows whether cache misses per operation rose with data size. The GC log or JFR gives GC time as a share of wall time, and container metrics show memory limits and swap. Running the workload at 1, 5 and 20 GB and plotting each metric shows which one bends upwards with the latency.

Java
// Object layout: 20 million Order objects, each with boxed fields
List<Order> orders; // about 40 to 60 bytes per order plus pointers
// Columnar layout: one primitive array per field
long[] orderIds = new long[20_000_000]; // 160 MB
int[] customerIds = new int[20_000_000]; // 80 MB
long[] amountsCents = new long[20_000_000]; // 160 MB
// Sorting or scanning one column reads memory sequentially, which caches and prefetchers love
Symptom and the factor it points to
What we seeLikely factorFirst check
Cache misses per operation rise sharplymemory hierarchy, pointer chasingperf stat or async-profiler hardware events
GC share of CPU or pauses growlive heap and allocation rate-Xlog:gc* or JFR
Memory far larger than raw dataheaders, boxing, lost compressed oopsjcmd GC.class_histogram
Swap use or container killsmemory inflation beyond limitscontainer metrics, OOMKilled events
Throughput drops as threads increasecontention, false sharing, NUMAlock profiling, per-core metrics

The usual fix is a layout change rather than a different algorithm. Primitive arrays, a columnar layout with one array per field, primitive collections libraries, or off-heap storage with the Foreign Function and Memory API cut object counts, make access sequential and shrink the GC's work. Sometimes the right answer is that 20 GB of data does not belong in one JVM's heap at all, and a database or columnar engine should do the work.

7Follow-up questions

Would a better algorithm help here?

Only if the profile shows the algorithm's operation count is the problem. In most cases like this, the operation count grew as expected and each operation got slower, so a cache-friendlier layout of the same algorithm wins more than a different algorithm with the same complexity on the same object graph.

Why does losing compressed oops matter so much?

Because references are everywhere in a Java heap. With compressed oops each reference takes 4 bytes; without them, 8. For an object graph full of references, the heap grows by roughly a fifth to two fifths, caches hold fewer objects, and the GC has more to scan. A heap of 31 GB can hold more useful data than one of 36 GB.

How would you test this before data reaches 20 GB?

With a scaling test: run the same workload on synthetic data at several sizes, such as 1, 2, 5 and 10 GB, and plot latency and the supporting metrics. Any metric that grows faster than the model predicts shows the problem before production does.

8Conclusion

In this answer, we saw that big-O assumes every operation costs the same, which stops being true when data outgrows the caches. From 1 GB to 20 GB, cache and TLB misses from pointer chasing, garbage collection over a much larger live heap, memory inflation from headers, boxing and lost compressed pointers, and contention between threads can each make latency grow far faster than n log n. We find the real cause with CPU and allocation profiles, hardware counters, GC logs and a scaling test, and the fix is usually a primitive or columnar data layout, or moving the data out of the heap.

What to say in the interviewAbout 90 seconds aloud

Open with

An O(n log n) algorithm going from 1 to 20 GB should get about 22 to 25 times slower, so if it is 100 times slower, something the model ignores has changed.

Then, if they want more

Big-O assumes every operation costs the same. First I would look at the memory hierarchy: at 20 GB almost every random access is a cache miss and often a TLB miss, and Java object graphs make it worse through pointer chasing, so the same number of operations each cost ten times more.

Second, garbage collection: marking cost grows with the live heap and the allocation rate, so I would check GC logs or JFR for GC CPU share and pauses.

Third, memory inflation: object headers and boxing can double or triple the raw size, and if the heap grew past about 32 GB we lost compressed oops, so every reference doubled; that can push the process into swap or a container memory limit. Fourth, contention: locks, false sharing and NUMA effects do not appear in single-thread complexity.

To find which it is, I would run the workload at several sizes and look at async-profiler CPU and allocation profiles, perf hardware counters for cache misses, and GC logs. The fix is usually a primitive or columnar layout rather than a new algorithm, or moving the data to a store built for it.

About this answer

Last updated
12 November 2026
Versions it names
Java 25
Checked
Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the official documentation for the version you use. What the checks cover
Where the question comes from
Written by Scrivio for practice. It is not taken from an interview.
Report an issue with this answer

Copy the line below and send it to the author with what you found. It carries the address of this page.

Issue with "Big-O says an algorithm is O(n log n) but your measured latency grows much faster as...": https://scrivio-interview-library.netlify.app/java/algorithms-and-data-structures/66aa00aab6/