Earlier quoted context omitted.
> … I didn’t realize the interview task was to re-implement someone’s PhD thesis in 30 minutes... What a bullshit task. I’m beginning to think this kind of interviewing should be banned. Seems to me it’s just an easy escape hatch for the interviewer/hiring manager when they want to discriminate based on prejudice.
Banning stupid interview questions is a bad idea for job applicants since they are such a good indication of bullshit job culture.
My Favorite Algorithm: Linear Time Median Finding (2018)
171–180 of 189 posts
Re: My Favorite Algorithm: Linear Time Median Finding (2018)
#172> On average, the pivot will split the list into 2 approximately equal-sized pieces. Where does this come from? Even assuming a perfect random function, this would be true only for distributions that show some symmetry. But if the input is all 10s and one 5, each step will generate quite different-sized pieces!
I think you answered your own question. It's the standard average-time analysis of Quicksort and the (unmentioned) assumption is that the numbers are from some uniform distribution. Why would the distribution have to be symmetric? My intuition is that if you sample n numbers from some distribution (even if it's skewed) and pick a random number among the n numbers, then on average that number would be separate the num…
In the pathological case where all the elements are the same value, one set will always be empty and the algorithm will not even terminate.
In a less extreme case where nearly all the items are the same except a few ones, then the algorithm will slowly advance, but not with the progression n, n/2, n/4, etc. that is needed to prove it's O(n).
Please note that the "less extreme case" I depicted above is quite common in significant real-world statistics. For example, how many times a site is visited by unique users per day: a long sequence of 1s with some sparse numbers>1. Or how many children/cars/pets per family: many repeated small numbers with a few sparse outliers. Etc.
Re: My Favorite Algorithm: Linear Time Median Finding (2018)
#173Earlier quoted context omitted.
Haha wow, this was definitely random. Thank you for letting me know, I'll take a look when I have the chance.
If I don't hear back in a week or so I will remind you, I hope that's OK with you. I'm aware your expectations may be low. Your thinking may be: if textbook authors such as John Taylor don't know the why, then why would some random dude know? The thing is: this is the age of search machines on the internet; it's mindblowing how searcheable information is. I've combed, I got to put pieces of information together that…
- I mean this kindly, but this is getting a tad bit aggressive. Please let me glance at my own pace (or not glance, if I don't find the interest to), rather than periodically following up with me around the forum to make sure I look at what you wrote. I was much more interested in this problem at the time I came across it than I was in 2019, and I was similarly more interested in 2019 than I am now. During this time I have both (a) come across other explanations that I've found ~semi-satisfactory, and (b) gathered other things to occupy my brain with. While I do get some enjoyment from the topic, refreshing my understanding of analytical mechanics is really not my topmost interest or priority right now. It could easily be months or years before I become interested in the problem again to even think about it.
- I find it rather... jarring to see a sentence like "Summing bars of signed area, in the limit of subdividing into infinitely many bars: that is evaluating the integral" in the middle of an explanation about Lagrangians and the principle of stationary action. Yes... integration is the limit of addition; you should hope your reader knows that well by now. You wouldn't re-teach "multiplication is really just repeated addition" in the middle of a lecture about Fourier series; this feels equally out-of-place. Not only does it make it seem like you don't know your audience, it also wastes the reader's time, and makes it hard for them to find any gems you might have shared.
- I took a quick look at your site and I don't actually see you attempt to explain the rationale for why the quantity of interest is T - V (as opposed to, say, T + V) anywhere. You mention "you are looking for the sweet spot: the point where as the object is moving the rate of change of kinetic energy matches the rate of change of potential energy", but... am I? Really? I certainly wasn't looking for that, nor have any idea why you thought I would be looking for that. It almost seems to assume what you were trying to prove!
Re: My Favorite Algorithm: Linear Time Median Finding (2018)
#174Earlier quoted context omitted.
I used to ask how to find the 10th percentile value from an arbitrarily ordered list as an interview question. Most candidates suggested sorting, and then I'd ask if they could do better. If they got stuck, I'd ask them which sorting algorithm they'd suggest. If they suggested quicksort, then I could gently guide them down optimizing quicksort to quickselect. Most candidates made the mistake of believing getting rid…
ETA: You said "used to" and I didn't acknowledge that. This is targeted at that kind of interview, not you directly. --- Had some "lucky" candidate stumbled upon an optimization you had never seen before, would you recognize it? If so, would you be honest and let them keep their discovery? After all, this isn't work for hire ... Moving on. Did these interviews reflect the day-to-day work these software engineers woul…
Among other things, I used to work on Google's indexing system, and also high volume risk calculations and market data for a large multinational bank, and now high volume trading signals for a hedge fund.
For instance, corporate clients of banks sometimes need some insurance for some scenario, but if you can narrow down that insurance to exactly what they need, you can offer them that insurance cheaper than competitors. These structured products/exotic options can be difficult to model. For instance, say an Australian life insurance provider is selling insurance in Japan, getting paid in JPY, and doing their accounting in AUD. They might want insurance against shifts in the Japanese mortality curve (Japanese dying faster than expected) over the next 30 years, but they only need you to cover 100% of their losses over 100 million AUD. You run the numbers, you sell them this insurance at a set price in AUD for the next 30 years, and you do your accounting in USD. (The accounting currency (numerare) is relevant.) There's basically nobody who would be willing to buy these contracts off of you, so to a first-order approximation, you're on the hook for these products for the next 30 years.
If you can offer higher fidelity modeling, you can offer cheaper insurance than competitors. If you re-calculate the risk across your entire multinational bank daily, you can safely do more business by better managing your risk exposures.
Daily re-calculations of risk for some structured end up cutting into the profit margins by double-digit percentages. Getting the algorithms correct, minimizing the amount of data that needs to be re-fetched, and maximizing re-use of partial results can make the difference of several million dollars in compute cost per year for just a handful of clients, and determines if the return-on-investment justifies keeping an extra structurer or two on the desk.
In order to properly manage your risk, determine what other trades all of your business are able to trade every day, etc., you need to calculate your risk exposure every day. This is basically the first partial derivatives of the value of the structured product with respect to potentially hundreds of different factors. Every day, you take current FX futures and forward contracts in order to try and estimate JPY/AUD and AUD/USD exchange rates for the next 30 years. You also make 30-year projections on the Japanese mortality curve, and use credit index prices to estimate the probability the client goes out of business (counterparty risk) for the next 30 years. Obviously, you do your best to minimize re-calculation for the 30 years, to incrementally update the simulations as the inputs change rather than re-calculating from scratch.
For the next 30 years, shifts in the Japanese mortality curves for the structured product desk affects the amount of trading in Japanese Yen and Australian Dollars that the FX desk can make, how much the Japanese equities desk needs to hedge their FX exposure, etc. You could have fixed risk allocations to each desk, but ignoring potential offsetting exposures across desks means leaving money on the table.
You can't sell these trades to another party if you find your risk calculations are getting too expensive. You are stuck for 30 years, so your only option is to roll up your sleeves and really get optimizing. Either that, or you re-calculate your risk less often and add in a larger safety margin and do a bit less business across lots of different trading desks.
I started asking this as an interview question when I noticed a colleague had implemented several O(N*2) algorithms (and even one O(N*3)) that had O(N) alternatives. Analysis for a day of heavy equity trading went from 8 hours down to an hour once I replaced my colleague's O(N*2) algorithm with an O(N) algorithm.
The point of the exercise is mostly to see if they can properly analyze the effects of algorithm changes, in an environment where the right algorithm saves tens of millions of dollars per year.
Granted, it's a bit niche, but not really that niche. I've been doing this sort of stuff in several different industries.
Re: My Favorite Algorithm: Linear Time Median Finding (2018)
#175Earlier quoted context omitted.
Where were you working? Sounds like you got lucky to work on some fun problems!
Sorry, but I'm trying to keep this account relatively anonymous to sidestep some of my issues with being shy. But, you're right, I was lucky to work on a bunch of fun problems. That period, in particular, was pretty amazing. I was part of a fun, collaborative team working on hard problems. And management showed a lot of trust in us. We came up with some very interesting solutions, some by skill and some by luck, that…
I totally respect that—don't let me pressure you any further.
Yeah, sounds like you had the ideal work setup: cool problem requiring cool technical solutions with good management. That's wonderful!
Re: My Favorite Algorithm: Linear Time Median Finding (2018)
#176Earlier quoted context omitted.
What's wrong with doing it in two passes? N iterations each doing 2 operations is exactly the same cost as 2*N iterations each doing 1 operation. Because multiplication is commutative.
It’s more like 2NM where M is loading the data from disk/memory. One pass is 2N+M. Why go to the store and back twice to buy two things instead of buying two things in one trip? ;p
Re: My Favorite Algorithm: Linear Time Median Finding (2018)
#177Earlier quoted context omitted.
Job interview question for an entry-level front end developer: "Reproduce the work of four Turing award winners in the next thirty minutes. You have a dirty whiteboard and a dry pen. Your time begins... now ."
And if you really want to impress, you reach into your pack and pull out the pens you carry just in case you run into dry pens at a critical moment.
Re: My Favorite Algorithm: Linear Time Median Finding (2018)
#178Re: My Favorite Algorithm: Linear Time Median Finding (2018)
#179FTA: “Proof of Average O(n) On average, the pivot will split the list into 2 approximately equal-sized pieces. Therefore, each subsequent recursion operates on 1⁄2 the data of the previous step.” That “therefore” doesn’t follow, so this is more an intuition than a proof. The problem with it is that the medium is more likely to end up in the larger of the two pieces, so you more likely have to recurse on the larger pa…
If I'm understanding correctly, the median is actually guaranteed to be in the larger of the two pieces of the array after partitioning. That means on average you'd only discard 25% of the array after each partition. Your selected pivot is either below the median, above the median, or exactly the median. If it's below the median, it could be anywhere in the range [p0, p50) for an average of around p25; if it's above…
Only in the first iteration. There’s a good chance it will be in the smaller one in the second iteration, for example.
So, your analysis is a bit too harsh, but probably good enough for a proof that it’s O(n) on average.
> Heavily skewed distributions would perform pretty badly
That’s why I used the weasel worlds “real world data” ;-)
I also thought about mentioning that skew can be computed streaming (see for example https://www.boost.org/doc/libs/1_53_0/doc/html/accumulators/...), but even if you have that, there still are distributions that will perform badly.
Re: My Favorite Algorithm: Linear Time Median Finding (2018)
#180 T(0) = 0
T(1) = 1
T(n) = n + T(n/5) + T(7/10*n)
We want to prove that: T(n) ≤ C*n
It is intuitive that T(a+b) ≥ T(a) + T(b), or in other words, T is superadditive. That can be shown by induction:Induction base: it holds for all a+b
T(0+0) = 0 + T(0) + T(0) ≥ T(0) + T(0)
Induction step: suppose it holds for all a+b T(a+b) = T(k)
= k + T(k/5) + T(7/10*k)
≥ k + T(a/5) + T(b/5) + T(7/10*a) + T(7/10*b)
= [a + T(a/5) + T(7/10*a)] + [b + T(b/5) + T(7/10*b)]
= T(a) + T(b)
Because T is superadditive: T(n) = n + T(n/5) + T(7/10*n)
≤ n + T(n/5 + 7/10*n)
= n + T(9/10*n)
Now we can apply the master theorem. Or to write out the proof (using a geometric series): T(n) ≤ n + T(9/10*n)
≤ n * ∑ᵢ₌₀ᶦⁿᶠᶦⁿᶦᵗʸ (9/10)^i
= n * 1/(1-9/10)
= 10*n
So, we have shown the algorithm is O(n) with C=10 (or less).