"A market cap has a fast half and a slow half."
The cost problem, and the wrong ways out
To rank the largest US companies you need a market capitalization for each one, across a universe of over twelve thousand listings. Market cap is price times shares outstanding. The naive implementation asks a provider for both, per company, every day — twelve thousand requests for a number that gets used once.
The usual escapes all degrade the answer. Sample a subset: now the ranking is approximate, and approximate rankings have exactly the failure mode this whole track is about. Refresh weekly: the prices go stale, and prices are the half that actually moves. Restrict to a pre-known list of large companies: you have assumed the answer you were trying to compute.
The decomposition
The escape is not to approximate — it is to notice that the two factors move on completely different clocks.
Price moves every session, and the provider offers a single grouped call that returns the day's bars for every listing at once. One request, twelve thousand prices.
Shares outstanding moves on filings — quarterly, in practice. It must be fetched per company, but it is nearly static, so it can be cached hard with a validity window measured in weeks.
So a daily pass costs one grouped call plus arithmetic. The expensive half is fetched rarely, the cheap half is fetched in bulk, and no company is dropped by random sampling. But be precise about what that buys: the ranking is exact arithmetic over the candidate pool, and the pool is a turnover-selected 1,500 out of roughly 5,100 priced common stocks. The step from there to "exact over the universe" rests on an assumption — that no top-500 company by market cap sits outside the top 1,500 by dollar turnover — which is very probably true and is nowhere verified. That is a much better position than sampling. It is not the same as exact, and a lesson in this quest of all places should not round it up.
The cheap net before the expensive catch
One more layer. Even cached, fetching share counts for twelve thousand companies is more work than needed for a top-500 ranking. So candidates are first narrowed by dollar turnover — price times volume — which arrives free in the same grouped call.
The reasoning is stated carefully in the code, and the care is the point: this is a wide net for a small catch, but a net, not a guess. It only has to be large enough that no top-500 company can fall outside it. Large companies dominate turnover by an enormous margin, so a candidate pool three times the size of the target is a very safe margin — and the safety is what makes it legitimate. A narrower pool chosen for speed would have been a sampling decision in disguise.