is a comprehensive book on getting a job at a top tech company, while focuses on dev interviews and does this for PMs.
CareerCup's interview videos give you a real-life look at technical interviews. In these unscripted videos, watch how other candidates handle tough questions and how the interviewer thinks about their performance.
Most engineers make critical mistakes on their resumes -- we can fix your resume with our custom resume review service. And, we use fellow engineers as our resume reviewers, so you can be sure that we "get" what you're saying.
Our Mock Interviews will be conducted "in character" just like a real interview, and can focus on whatever topics you want. All our interviewers have worked for Microsoft, Google or Amazon, you know you'll get a true-to-life experience.
A different approach. Code is Perl:
Here's why this works. The explanation is simpler if we assume that N is a power of 2.
Note that if a list has the property that the average of any two items in the list does not appear in the list between them, then so does any list derived from that list by adding or subtracting a constant from all items in the list. In the following, I'll be working with lists that have had 1 subtracted from each element.
The algorithm starts with the solution for N=1, which is (0). It then iteratively generates the solution for 2N from the solution for N by the following procedure: replace each item x in the list for N with a pair of items, N+x and x.
So, to go from N=1 to N=2, the 0 in (0) is replaced with 1, 0, giving (1, 0).
To go from N=2 to N=4, the 1 in (1,0) is replaced with 3, 1, and the 0 is replaced with 2, 0, giving (3, 1, 2, 0).
This gives us the sequence of lists for various N:
N=1 (0)
N=2 (1, 0)
N=4 (3, 1, 2, 0)
N=8 (7, 3, 5, 1, 6, 2, 4, 0)
...
This can be viewed as a binary tree, with the root being the 0 from (0). It's children are the 1 and 0 in (1, 0). The children of the 1 in (1, 0) are the 3 and 1 in (3, 1, 2, 0), and so on.
Observation: if two items, x and y, in a given row have a common ancestor z in an earlier row, then all items between x and y also have z as an ancestor. By "between" I mean between them in the list order, not numerically between. E.g., 1 is between 3 and 2 in the N=4 list.
Observation: if two items, x and y, in a given row have a common ancestor z in row N=k, then x and y are congruent to z mod k.
Now let's consider any two items, x and y, in the final row, and let z be their closest common ancestor and let z be in row N=k. If k=1, then one of x is even and one of x is odd, and so their average is not an integer and so is not in the final row.
If k != 1, then x and y are congruent to z mod k. Since z is the closest ancestor of x and y, one of them is part of the left subtree of z, and one is part of the right subtree of z. That means that one of them is congruent to z mod 2k, and one is congruent to k+z mod 2k.
Hence, the average of x and y is congruent to z+k/2 mod 2k, which means that it is also congruent to z+k/2 mod k. This shows that the average of x and y is not between x and y, because as we noted earlier, if it were between x and y, then it would have to have z in row N=k as an ancestor, and so would have to be congruent to z mod k.
Implementation note: if N is not a power of 2, you can just proceed as if it is a power of 2, and simply omit actually putting any numbers bigger than N in your list on each iteration, so you don't end up wasting space storing numbers that you aren't going to need. I do this in the Perl code above.
More concisely, but not quite as space efficient:
- tzs February 03, 2012