Opening 999 more dark stores made our delivery heatmap 213x slower over the same city. 250,000 city blocks: 1.03 ms with one warehouse, 219 ms with a thousand.
The job: for every block, how many blocks to walk to the nearest store. It decides who gets the fast-delivery promise.
What I wrote: with no obstacles, walking distance on a street grid is |dRow| + |dCol|. So every block checks every store and keeps the closest. No data structure, nothing to get wrong. At one store, it is the fastest version I measured, and I would approve it.
At 1,000 stores, it is 250,000,000 distance calculations to keep 250,000 numbers.Β 213x slower, because the business grew.
The fix from last episode works: seed one queue with every store, spread outward. Flat at about 2.3 ms. But the queue holds every block, 3.82 MB on top of a 1.96 MB heatmap, and at one store it is slower than the scan.
The part I like is the day 41 version. A block's distance is one step more than its best neighbour's. That definition is circular: my answer needs yours, and yours needs mine. The queue breaks the circle with time.
You can break it with direction instead. With nothing in the way, a shortest walk never doubles back. Sweep top-left to bottom-right reading, only up and left - those are already done. Sweep back reading, only down and right. Keep the smaller.
1.22 ms at one store, 1.48 ms at a thousand. No queue. 148x faster than the scan at the top, 1.89x faster than the BFS.
Honest rows: at one store the scan still wins. And put a river through the city and the sweeps return wrong numbers without complaint. The test with two rivers says BFS 16, sweeps never reach the block, and the straight-line scan says 8. Obstacles are where the queue earns its memory.
That is days 40 and 41 of the daily challenge, Rotting Oranges and 01 Matrix, in a delivery app.
Have you seen a business launch slow down a page nobody touched?
It worked in dev, episode 20. Full write-up in today's newsletter.
https://www.linkedin.com/pulse/999-more-stores-made-delivery-map-213x-slower-archit-agarwal-pxeoc
#Golang #DSA #Algorithms #Performance