Series – Coding Interview Book (part 1)

So, I have started to have a look at the Coding interview book. Have been reading about how interviews happen at top 5 IT firms like Google, Amazon, Apple Inc etc.

To think, or even dream, I could be part of such awesome organizations is like a very very far fetched thought. Wouldn’t have my hopes so high ever.

Then again, that doesn’t mean, I shouldn’t start practising some cool coding questions for some interviews. In the first few pages, I found out a few easy questions, which I will try to write code of and put it somewhere online for everyone’s review. Yeah! Together we learn 😛

The questions/sample exercises seem to be pretty easy. So here we go!!!
1. Design an algorithm to sort a list
Question: What sort of a list? An array? A linked list?
Answer : An array.
Question : What does the array hold? Numbers? Characters? String?
Answer : Numbers.
Questions: And are the numbers integers?
Answers : Yes.
Question : Where did the numbers come from ? Are they IDs ? Values of something ?
Answers : They are ages of customers.
Question : And how many customers are there ?
Answers : Around a million.

I will leave some analysing of the question to you guys. Let me just quickly write down some code for solving the problem.

So the code is available here magic_sort.rb You guys can have a look, I am using the Benchmark module of ruby to capture the execution time. As always insertion sort is faster for smaller arrays. But when the array size increase beyond 10000, the performance is significant for our magic sort 🙂
Have a look and let me know, if we can improve it even further.