|
On June 25 2011 09:45 Oracle wrote:Show nested quote +On June 25 2011 09:33 ptell wrote: Solution to 7) :
First note that every card can be mapped to a number in the range of 1 to 52. Using the convention in bridge, the map is (from smallest to highest):
01 <--> Ace of Spades 02 <--> Ace of Hearts 03 <--> Ace of Diamonds 04 <--> Ace of Clubs 05 <--> Deuce of Spades . . . 51 <--> King of Diamonds 52 <--> King of Clubs
Then the first four cards corresponds to a set of four number, e.g. {04, 13, 17, 31}. Then reindex the remaining 48 cards so that it starts from 01 and ends at 48.
Now there are 4! = 24 ways to arrange four distinct cards so we can map to indices 01 to 24. Next to represent the indices 25 to 48, flip the card with the smallest value. Yup, can't find a problem with it. I came across this solution too but didn't like it because most likely the observer would need a coding grammar to lookup which permutation refers to which.
Ok, non math person here, but I think the posted #7 solution relies too heavily on knowledge of Bridge convention. Mine (if I even understand your post correctly) hinges on knowledge of the alphabet, but I'd wager that's a far greater set of people.
![[image loading]](http://i848.photobucket.com/albums/ab46/apettythief/2011-06-24_21-00-36_524.jpg)
I haven't got the best draw of four determining cards here, but the fifth card is + Show Spoiler +.
Here's how you can tell. [spoiler]Two cards are face-up: therefore their faces must contain relevant information. This information is limited to value and suit.
Two are face-down. One of these is oriented counter to everything else: therefore their faces do not contain useful information and are being used to indicate something else. Let's take the most-different card first, in the top-left.
The array of all possible cards (it's invisible, and the table is clear, we're not crazy) ends at the corner of this card. It's differing orientation, face-down-ness, and location outside of the array let us mark it as an origin point. Conversely, we could mark it as a card that has nothing entirely in common with the others- the point is: it's meant to indicate that the locations of the other cards fit into an array. Hopefully either this card or the particular positioning of the other cards will get that to the observer, because it's essential.
The other face-down card is in the array, and it is showing no value (in that it's face-down). It indicates that this is the particular droid we're looking for- it marks a location in the array (which would be filled by our fifth card).
The array itself is a bit more difficult to outline (especially given these cards- ideally we'd have no two cards in the same suit). The information/value contained on the face of the cards is limited to suit and number. In this array, suit is indicated by vertical position, and number by horizontal position. I bold this because it's pretty much the whole solution. In our example we don't get to see the full suit arrangement, which invites the observer to err in determining the fifth card based on it's being the sole member of the set of it's particular suit [Edit: nevermind, the picture is of my second set of cards, sorry. We still don't see the whole suit arrangement, but with anything less than 40 cards that's not a guarantee anyway.].
Hence my alphabet comment: I've elected to order the suits in alphabetical order by name in English. The orthographs for the suit names match those used on the face cards (not that there necessarily are any, but...), and the simplest organization for these letters is the alphabet. Thus, vertically, our array is Clubs>Diamonds>Hearts>Spades.
Summary: the face-up and sideways face-down card indicate the arrangement, and the face-down card indicates the particular location we want to communicate. No math needed, only basic card skills. Sorry for the terrible picture.
EDIT: MOTHER %^&#@* I can't read. >_<
There is only enough space on the table to arrange the cards vertically, side by side. All four cards must be at the table and cannot be re-ordered once set.
|
5)
Let a_1, a_2, b_1, b_2, c_1, c_2, ... be the pairs and x the unique element.
^ = bitwise XOR: (a_1 ^ a_2) ^ (b_1 ^ b_2) ^ (c_1 ^ c_2) ^ x = x because a ^ a = 0 and 0 ^ a = a. Since ^ is commutative and associative the order doesnt matter and to get the unique element we can just run in a loop over all elements and XOR them. Should work =)
|
On June 25 2011 20:45 DeLoAdEr wrote: 5)
Let a_1, a_2, b_1, b_2, c_1, c_2, ... be the pairs and x the unique element.
^ = bitwise XOR: (a_1 ^ a_2) ^ (b_1 ^ b_2) ^ (c_1 ^ c_2) ^ x = x because a ^ a = 0 and 0 ^ a = a. Since ^ is commutative and associative the order doesnt matter and to get the unique element we can just run in a loop over all elements and XOR them. Should work =)
That's exactly correct, good job
|
1) I don't think this is possible. List and array-backed queues have linear time inserts, and heap-backed queues have logarithmic extracts. If we don't need constant time extracts, heaps would work fine.
4) Median of medians algorithm. God I love this one.
|
On June 26 2011 00:22 BottleAbuser wrote: 1) I don't think this is possible. List and array-backed queues have linear time inserts, and heap-backed queues have logarithmic extracts. If we don't need constant time extracts, heaps would work fine.
4) Median of medians algorithm. God I love this one.
#1 Oops, youre right I mistyped the question. Should be possible now.
#4 Median of Medians can be generalized to k-select, but the tricky part is how
|
6) Initialize a counter k as 1, and save your current location as x. Go to the next node k times and check if its the same as your current location. Until it is, continue to save your current location as x, and increment k, and check if k hops ahead is the same as the current location.
This is O(n) because if we enter a loop, it is at most size of n. When k reaches a multiple of n, we are done, so it is O(n).
1) A min and a max heap of equal sizes. Ta-da.
|
Median of medians: group the elements into groups of 5, and sort the groups of 5 (n/5*5log5)=O(n). Take the median of these recursively (O(n), ok, that's cheating, because I don't remember and can't understand Wikipedia's proof... should do that ... later...).
Now you can split the array into an upper and lower half by comparison with our pivot. If the upper half's size is k-1, then we can return the median. If k>size/2-1, then k-=size/2+1, and we do it again for the lower half. If k<size/2-1, then we do it again for the upper half. This recursion is also O(n), somehow.
|
On June 26 2011 01:14 BottleAbuser wrote: 6) Initialize a counter k as 1, and save your current location as x. Go to the next node k times and check if its the same as your current location. Until it is, continue to save your current location as x, and increment k, and check if k hops ahead is the same as the current location.
This is O(n) because if we enter a loop, it is at most size of n. When k reaches a multiple of n, we are done, so it is O(n).
1) A min and a max heap of equal sizes. Ta-da.
#1 correct. Theres also another method using AVL trees and keeping the median at the root.
#6 Im actually not sure how when k reaches a multiple of n we are done. Is it not when k reaches the length of the cycle we are done? My understanding of that algorithm is O(pn) where p is the size of the cycle. Either way, try to find precisely where the cycle begins (i know the hint algorithm I gave doesnt do this, but the question asks for this)
|
Yar, I forgot about that requirement and fixated on the hint algorithm. Shit me. And you're right, it's O(pn) or O(n^2) if the whole damn thing is a cycle. I have to go back to school.
I can still fix the algorithm I gave (O(n^2) still); you backtrack by k-1 steps, see if k steps ahead is the same as that node, and continue (backtrack by k-2, k-3, and so on and see if k steps ahead of that node is the same) until you reach one that isn't.
But then again, that only works for doubly linked lists. Grr.
|
On June 26 2011 01:39 BottleAbuser wrote: Median of medians: group the elements into groups of 5, and sort the groups of 5 (n/5*5log5)=O(n). Take the median of these recursively (O(n), ok, that's cheating, because I don't remember and can't understand Wikipedia's proof... should do that ... later...).
Now you can split the array into an upper and lower half by comparison with our pivot. If the upper half's size is k-1, then we can return the median. If k>size/2-1, then k-=size/2+1, and we do it again for the lower half. If k<size/2-1, then we do it again for the upper half. This recursion is also O(n), somehow.
Heh, im not entirely convinced 
On June 26 2011 01:49 BottleAbuser wrote: Yar, I forgot about that requirement and fixated on the hint algorithm. Shit me. And you're right, it's O(pn) or O(n^2) if the whole damn thing is a cycle. I have to go back to school.
I can still fix the algorithm I gave (O(n^2) still); you backtrack by k-1 steps, see if k steps ahead is the same as that node, and continue (backtrack by k-2, k-3, and so on and see if k steps ahead of that node is the same) until you reach one that isn't.
But then again, that only works for doubly linked lists. Grr.
Dont worry, these are meant to be challenging... Really focus on the first hint, the solution is actually very very simple to implement, and not even that conceptually complex.
Also, this solution assumes that the start of the cycle has its backward pointer to the linear list, and not to the cycle
|
If it is a doubly linked list, then my solution still works in O(n^2), because you're guaranteed to find the place where you jumped into the cycle. From there, you have at most k-1 nodes in between a definite non-cycle node and a definite cycle node, and you can check each one to see which is the first. I'll give these another crack tomorrow, it's drinkin time. And what the heck is 0xabc and 0xabf? Those are different values, unless C++ has some really weird quirks...
|
On June 26 2011 02:07 BottleAbuser wrote: If it is a doubly linked list, then my solution still works in O(n^2), because you're guaranteed to find the place where you jumped into the cycle. From there, you have at most k-1 nodes in between a definite non-cycle node and a definite cycle node, and you can check each one to see which is the first. I'll give these another crack tomorrow, it's drinkin time.
And what the heck is 0xabc and 0xabf? Those are different values, unless C++ has some really weird quirks...
Yes but what if the backwards pointer points to the previous element of the cycle instead of the linear list? Ie A<->B->C<->D<->E<->C (Notice that C doesnt point back to B, but instead to E)
To answer the second part,
they are hexidecimal values, so the address at 0xabf is exactly the same as the address at 0xabc according to the C++ interpreter.
http://www.wolframalpha.com/input/?i=0xabf
http://www.wolframalpha.com/input/?i=0xabc
(Really because C++ assumes every address must be a multiple of four, and hence it takes the lowest multiple of four in any address)
Also, it might be helpful to forget what you know about pointer arithmetic
|
If the back pointer is different that makes it too easy so ill discount comparing self and self.next.previous.
2) Give each edge unity weight and run a shortest path k times, removing traversed edges as you go. This works because if we find that start and end disconnect after x iterations, we can remove x edges from G to cut it by umm really carefully selecting an edge from each found parh and Mengers says that thats that. Hah.
|
Proof of median of medians linear runtime
Finding the median of medians takes a recursive call on the sets of 5, or T(n/5).
Partitioning into "this group of 5's median is less than/greater than median of set" takes linear time or O(n).
We are guaranteed that the elements of each goup of 5 in the groups whose median is less than set median are also less than the set median (transitivity of less than operator). Therefore, at least 3/5 of 1/2 of the set is less than the set median, and no more than 1-3/10. In the wost case, T(n*7/10).
Now, T(n*7/10)+T(n/5) <= T(n*9/10). Thus, worst case is T(n) <= O(n) + O(n*9/10) + O(n*(9/10)^2 + ...) = O(n) * (1 + 9/10 + (9/10)^2 + ...) which is O(n)*c for some constant c. Power series of 9/10^n for n in Z converges on 10, so c is truly asymptotically constant.
|
On June 26 2011 23:07 BottleAbuser wrote: Proof of median of medians linear runtime
Finding the median of medians takes a recursive call on the sets of 5, or T(n/5).
Partitioning into "this group of 5's median is less than/greater than median of set" takes linear time or O(n).
We are guaranteed that the elements of each goup of 5 in the groups whose median is less than set median are also less than the set median (transitivity of less than operator). Therefore, at least 3/5 of 1/2 of the set is less than the set median, and no more than 1-3/10. In the wost case, T(n*7/10).
Now, T(n*7/10)+T(n/5) <= T(n*9/10). Thus, worst case is T(n) <= O(n) + O(n*9/10) + O(n*(9/10)^2 + ...) = O(n) * (1 + 9/10 + (9/10)^2 + ...) which is O(n)*c for some constant c. Power series of 9/10^n for n in Z converges on 10, so c is truly asymptotically constant.
Ok so now you have the median element, and id assume you want to partition the array around it, which clearly takes O(n). Now what? Recursively pivoting the section with k in it isn't O(n), in fact it is o(nlogn) (think binary search/quicksort)
For #2, Using Prims algorithm or Djistkras algorithm to find an AC-path may work, and removing the path each iteration, but I am not so sure how we are guaranteed to find k of them, since a shortest path may intersect an edge of another path, and removing it may reduce remaining number of AC paths.
|
On June 25 2011 09:33 ptell wrote: Solution to 7) :
First note that every card can be mapped to a number in the range of 1 to 52. Using the convention in bridge, the map is (from smallest to highest):
01 <--> Ace of Spades 02 <--> Ace of Hearts 03 <--> Ace of Diamonds 04 <--> Ace of Clubs 05 <--> Deuce of Spades . . . 51 <--> King of Diamonds 52 <--> King of Clubs
Then the first four cards corresponds to a set of four number, e.g. {04, 13, 17, 31}. Then reindex the remaining 48 cards so that it starts from 01 and ends at 48.
Now there are 4! = 24 ways to arrange four distinct cards so we can map to indices 01 to 24. Next to represent the indices 25 to 48, flip the card with the smallest value.
I've given this a little more thought and actually came to a flaw; how would the observer know how to do an index shift if the final card is upside down?
|
The recursive calls sum to O(n). If we're generous and say that each call cuts the size in half, then we take O(current size) time for each recursion. So we get T = n*(1 + 1/2 + 1/4 + 1/8 + 1/16 + ...) = 2n. However, even if we're not generous our power series is of 9/10 instead of the much nicer 1/2, but it still converges on 10n, instead of nlogn.
Put another way, the recursion stack depth is indeed dependent on n, ie it's log(n). However, the sum of the lengths that we are actually looking at is linear to n.
|
1) Implement an integer Median Priority queue. That is, Peek has O(1) and Insert/Delete Median has O(logn). (Data structures course). I vaguely remember something from Wikipedia about a heap that alternates between min and max heap at every level that has the desirable properties.
2) Suppose there is a street-race, with a total of k contestants. You start at intersection A, and finish at intersection C. For safety, you want to plan this so that no two contestants can possibly be on the same road together. (Their routes are unique) There are exactly k unique routes from A to B (by unique I mean no roads in any two routes are the same) and exactly k unique routes from B to C. Describe a method to find k unique A-C routes. (Graph theory course) I think I'm missing something here.
3) In OOP, how would you create/modify a class such that it can ONLY be allocated on the heap. That is, allocation onto the stack will error or fail. (Bloomberg) Private constructor and a named constructor idiom is the classic way to do it, but a nicer way to do it is to use a private destructor (or a protected one if you still want inheritance), and explicitly deleting via the base class:
class Base { public: explicit Base () {} virtual ~Base () {} };
class Derived : public Base { public: explicit Derived () {} protected: virtual ~Derived () {} };
int main (int argc, char* argv[]) { // Derived x; // fails Base* base = new Derived(); delete base; Derived* derived = new Derived(); delete dynamic_cast<Base*>(derived); }
Edit: This is possible as well:
class Test { public: explicit Test () {} void destroy () { delete this; } protected: virtual ~Test () {} };
int main (int argc, char* argv[]) { Test* test = new Test(); test->destroy(); }
There is a related concept I call pseudo abstract classes, where the base class has protected constructors and destructors to prevent instantiation, and you can only create objects of the derived class.
4) Given an array of size n, implement a function which will find the kth largest element in O(n) time and O(1) space. Be careful to note that elements are not necessarily integers, but have the standard comparison operators (ie <=, <) defined, all of which are transitive. (Data structures course) Median of medians can find the median in O(n) time, you can recurse on the left or right half of the vector to find the desired element. Equal elements can mess things up, but you can work around them by wrapping them in an std:: pair, with the second element being an arbitrary ordering of the elements.
5) Given an integer array of size 2n+1 (odd length), where every element exists in pairs except for one unique element, find the unique element in O(n) time and O(1) space. (Google) Nice, the solution for this one eluded me. Should clarify that the elements in pairs are the same.
6) Find the start of a cycle in a C++ linked list in O( n ) where n is the number of nodes, and space complexity O(1). Elements in the array are not necessarily distinct, so searching for the first recurrence of an element does not work. You do not know n. (IBM) I've heard this puzzle before. All you have to do is start two search, the first one steps one at a time, the second one steps two at a time. If they ever point to the same element, that element is in the cycle (or is the start of the cycle?). Ah yes, it is in the hints. Well, it is possible with only one pointer if we assume that all pointed objects are aligned, so we can store extra information in the pointers like mark bits, and that these mark bits are unset; we simply walk through the linked list, marking visited pointers until we encounter an already marked pointer.
7) Given a standard 52 card deck, you pull out 4 arbitrary cards. Now, you pull out the next card and memorize it before putting it back in the deck. Describe a method to arrange the first four cards in any order on a table so that someone who knows your system will be able to identify the 5th card you pulled. There is only enough space on the table to arrange the cards vertically, side by side. All four cards must be at the table and cannot be re-ordered once set.
Clarification: You may flip cards upside down, but note that the observer will not be able to see what the upside down cards are.
Well, if you define an (otherwise arbitrary) ordering on the cards, you can convey a lot of information with arranging cards, and flipping some of them upside down:
0 upside down card in 4 choose 0 positions and 4! possible ordering of visible cards = 4 choose 0 * 4! = 24 1 upside down card in 4 choose 1 positions and 3! possible ordering of visible cards = 4 choose 1 * 3! = 4 * 6 = 24 2 upside down card in 4 choose 2 positions and 2! possible ordering of visible cards = 4 choose 2 * 2! = 6 * 2 = 12 3 upside down card in 4 choose 3 positions and 1! possible ordering of visible cards = 4 choose 3 * 1! = 4
24 + 24 + 12 + 4 = 64 possible arrangements, more than enough to identify that 5th card.
|
Frigo, I don't agree with your solution for 6. If you use 2 pointers, you can know when you're in the loop, but you don't know where the loop starts. If you use a marker on each node, you're using O(n) space, not O(1). The hint actually makes me think this is the answer the OP was looking for, but you have no guarantees about the starting pointer values at all: for any valid address, there are 4 values that will resolve to that address (n, n+1, n+2, n+3), but which one indicates that this was already visited, and what guarantees that this one didn't just start with that value?
|
Well, the space requirement is dubious. Either O(1) if we assume that the unused space is already given, or O(n) if we don't. I don't see how could you use only one pointer without exploiting this. I am sure the OP was thinking about this approach as well, the hint is a giveaway; most C++ compilers align objects and won't return non-aligned address that could screw up the algorithm.
Determining where the loop starts is trivial if you know any multiple of the loop length. This requires an additional counter though.
|
|
|
|