News for hackers. Stuff that matters.
Re:Sorting 1 million 8-digit decimal numbers in 1MB of RAM by ChuckMcM on Sunday October 21, 2012 @03:44PM (#4680552)
You've missed the second part, which is reading out the sorted list. By storing which numbers you've seen, in a virtual bit field you have 'codified' in the index of the bit field the seen number. Thus to 'print the sorted list' you simply go through the bit field starting at index zero and print any index in the bit field that is a 'one'. That is your sorted list of numbers.

The reason this was an interview question was because it allowed the interviewer to see if the candidate could see 'past' the requirement of sorting (which pre-loads your mental algorithm cache with all sorts of algorithms) to the somewhat more subtle concept of embedding partial computation into the data structure.

Early in my career at Sun I was tasked with putting mandatory file and record locking into UFS in the kernel, the challenge had always been how do you avoid deadlock? Of course one level deadlock was fairly easy, you need to know if process A is holding a lock and waiting on process B which is holding a lock elsewhere, and you are in the context of process B trying to get this lock then you can see that you will deadlock A. But 'n' level deadlock starts getting harder because the possibilities multiply. I discovered that you could create a doubly linked tree structure which had modest memory growth per-process but any process could walk it one way and look for 'itself' on the list to discover if there was a deadlock possible. Here the 'code' was walk a list and note that you were already on it, so you would deadlock if you got the lock.

The number 'sorting' problem is very much like that because while numbers have different values, they are intrinsic values. Unlike strings which consist of an arbitrary sequence of tokens. So the goal of the question was to identify people who could see the difference between 'sorting numbers' as a problem and 'general sorting.'

Re:Sorting 1 million 8-digit decimal numbers in 1MB of RAM by jules on Sunday October 21, 2012 @10:06PM (#4681531)
Your solution still doesn't in any way fit into the required space...for a random sequence there will be hardly any coalescing going on, and you'll blow way past 1MB with the method as described.