Code WorkOut of the Day -- Daily exercises for programmers looking for jobs, or just to keep sharp with skills and have fun.
I give talks, like this: https://youtu.be/NpvTE7GlXSM for people looking for jobs, or groups of programmers preparing for M&A tech HR due diligence.
Follow us on twitter: @codewod

How to

When solving these problems, learning by doing is much better than learning by reading. I encourage to you read only as far in the solution as you need, then trying to solve the problem. If you get stuck, try reading a little further. And of course, let me know if you find a better solution!

Thursday, February 13, 2014

Question: How do you sort socks most efficiently from a pile?

Question: How do you sort socks most efficiently from a pile?

Solution: There are a bunch of assumptions here, so let's start listing them. First, - let's not assume that all pairs are distinct (e.g., you could have multiple pairs of the same socks). - also, let's assume that there is no distinction between left foot/right foot for socks, (initially) - that the number of socks pairs is a reasonable number of socks, (e.g., less than thirty), - the socks can quickly be compared for distinctness - you have sufficient table space for many pilesLet's start by thinking about how we do this in real life. What I do when comparing socks, is I pick up one, and then go through the remaining socks to find the match. In worst case, this requires: (n-1) comparisons, then another (n-3) comparisons, then (n-5)...Well, this is an O(n^2) algorithm. So, this seems pretty slow. But, when I do it, it certainly isn't this slow, as the algorithm has a few optimizations which aren't captured here. When I think of going through my socks, I somewhat remember my socks after the first pass through. For example, I might remember that my blue sock was at the end, so I don't really have to look at all of the socks to figure out where this one goes.Also, a comparison isn't really going through each sock one-by-one. My vision/brain lets me look at many socks at once, say 10, and quickly find the match. Some socks even stick out. So, the problems is in reality, much faster than n^2, given these optimizations.So, if I include these optimizations into the way that I sort socks, you could imagine that it's the same algorithm: - Prepare your sorting by moving the socks into subpiles of 10, with all socks visible - Pick up a sock, compare it until you find the matchWith this optimization, the number of potential lookups is reduced by a factor of 10 (socks divided into 10s), and probably more given our memory of where socks are. Given that we assume that we have fewer than 30 pairs of socks, finding a match is fewer than 6 look-ups, even not including memory. If we include a 2x improvement with memory, this probably makes it under 3. This is almost constant time for each sock, and it goes down as we pair socks. This is pretty darn good.Now, let's imagine any other optimizations that might exist. First, we could look to speed up the set-up process, by potentially spreading our socks out quickly and not being too rigorous on the number of socks in each pile. Or, we could consider moving socks into subpiles if they can easily be divided into distinct groups (e.g., blue vs. white) to reduce the lookup set. Perhaps we could get out of the problem all together, by doing something like using sock-clips when we wash our socks.Also, some people have suggested 'sorting' the socks in order to solve this problem, but this is a challenging problem as socks have no clear, easily observable order (unlike numbers). So, it's very hard to know if a red sock goes before blue, or after red. And, even normal ways of comparing (e.g., some sort of color or length) gets difficult given real-world conditions like lots of white or dark blue socks of similar size, which makes the comparison operator complex and time-consuming.In it's more generic form, the problem is something like, given n objects where each object in the set has a distinct pair, how can you most quickly sort the objects into pairs. We'll do this generic form next time.<Please send bugs or optimizations!>Thank you to Greg D. for suggesting this problem.

No comments:

Post a Comment

30 minutes, once a week, free

To help the community, I do one 30 minute interview every week for free for candidates (virtual, over some online coding tools). Email me if you're interested. It's good practice for the kinds of questions you'll get, and I'll give honest feedback as to your skills, likelihood of getting a job, and tips for improving/practicing for future interviews.

I especially like to work with minority candidates, and those looking to make career changes, so please shoot me an email, provide some background, and we'll set up a time.

noah@codewod.com

Also, if you find alternative or better answers to the questions, please let me know and I'll post/credit!

About Me

About me: I'm currently the founder of Workhood (www.workhood.com) and have spent over 10 years in the technology industry. Prior to Workhood, I was the co-founder of SocialShield, acquired by Avira (A/V security company with over 100m users), where I was subsequently the VP Technology/Operations. I worked previously in McKinsey's technology practice in London and New York and also worked at several start-ups as an engineer and product manager, founding my first venture backed company when I was 22 -- and have raised over $35M in my career in financing. My focus is always on delivering great products quickly and I am a huge proponent of agile programming/product development and the lean start-up techniques. I'm a Stanford CS grad, always interested in coding problems -- co-author of the best-selling book: Programming Interviews Exposed: Secrets to Landing Your Next Job. I also went to HBS and am an avid college football and soccer fan.

I blog about coding problems at: www.codewod.com and about start-ups at www.noahkindler.com. I live in the Bay Area and can often be found at Crossfit or on the slopes when I manage to sneak out.