by bqe · 5y ago · 0 comments
The reason I made this was an old Amazon interview question. The question was basically, "Find the median of a huge data set without sorting it," and the "correct" answer was to have a fixed size sorted buffer and randomly evict items from it and then use the median of the buffer. However, a candidate I was interviewing had a really brilliant insight: if we estimate the median and move it a small amount for each new data point, it would be pretty close. I ended up doing some research on this and found P^2, which is a more sophisticated version of that insight.
[1]: https://arxiv.org/pdf/1902.04023.pdf / https://github.com/tdunning/t-digest
Yeah, that was one of the reasons we chose it as one of the ones to implement, seemed like that was a really interesting tradeoff, we also used uddsketch[1] which provides relative error guarantees, which is pretty nifty. We thought they provided different enough tradeoffs that we wanted to implement both.
https://github.com/timescale/timescaledb-toolkit/blob/main/e...
(The TimescaleDB Toolkit is also implemented in Rust)
Interesting will have to take a look! Thanks for sharing!
[1] http://www.numericalexpert.com/articles/single_pass_stat/
[0] http://dimacs.rutgers.edu/~graham/pubs/papers/bquant-icde.pd...
So, not find, but approximate. That's a different thing.
(that you sort yourself)
Is the storage restriction the point?
Thanks for sharing! Hadn't heard of that algorithm, have seen a number of other ones out there, we chose a couple that we knew about / were requested by users. (And we are open to more user requests if folks want to use other ones! https://github.com/timescale/timescaledb-toolkit and open an issue!)
I had a basic screening call fail once because the expected answer was (in my perspective) more naive than my answer. I'd love it if generating curiosity were an interview +1.
Yeah, this is gradient descent on the absolute loss.
You can use eg QuickSelect (https://en.wikipedia.org/wiki/Quickselect) or Median of Medians.
They don't sort the data, but they do need linear amount of storage.
Isn't this done using a min heap and a max heap in conjuction?
1. huge data set: heaps requires storing the whole set, but "huge" means "more than you can store"
2. without sorting it: heaps are basically semi-sorted, so you are breaking the rules
Really? Where's it coming from?
With a few exceptions this is pretty common scenario.
And possibly it's live data.
The original problem is probably more accurately described as “what if you have too much data to sort” though.
I agree that if you have the whole thing, doing heapsort and pulling a[N/2] or a[1 + N/2] is simpler.