A Coding for Interviews weekly practice problem group member mentioned during our Skype call that reading through the Java collections library was the most valuable step he took while preparing for his programming interviews. In addition to getting a better understanding the standard data structures, hearing a candidate say “well the Java collections library uses this strategy…” is a strong positive signal.
Indeed, other HNers echoed the sentiment:
Wael Khobalatte writes (link added),
I usually have a fun time going from method to method just exploring any of the implementations in that library. Josh Bloch (One of the writers of the library I think) uses a lot of examples from the Java collections in his book “Effective Java“, which I also recommend to everyone.
And dev_jim provides a great suggestion,
It helped me when I was a bit rusty with data structures and was looking for a new job recently. And if you can try and re-implement the data structures in a different way. For example, the JDK HashMap uses chaining so try and build an open addressing one. Not only does it teach you about data structures themselves but it gets you practicing coding very quickly for these toy coding problems that get thrown at you during interviews.
Reading the standard libraries of your favorite programming languages is really worth it. Give it a shot. Here’s why.
Learn the best practices and idioms
Reading standard libraries can not only show you what is available, it often teaches you how to write Good Code™ in your language as well.
- Do you include newlines after blocks?
- How are variables, classes and methods normally named?
- What state is kept and why?
- How and when do enums, structs and plain old classes get used?
Know your language better
As a Ruby programmer, reading the standard libraries of Ruby and Ruby on Rails has been especially enlightening.
Writing Rails for the first time, it feels like you are writing pseudo-code.
Helpful::Application.routes.draw do use_doorkeeper do controllers :applications => 'oauth/applications' end if Rails.env.development? require 'sidekiq/web' mount Sidekiq::Web, at: "/sidekiq" end root to: 'pages#home' end
- Where is this function
- What makes the use_doorkeeper code’s call to controller different than another?
- From another example: why and how does
User.find_by_favorite_food('pizza')have the variable name
foodembedded inside of the method name?
Beginners will make mistakes that seem bizarre for people who have programmed before—calling methods with the names of methods or classes, declaring symbols instead of making function calls, forgetting to pass a block to something that requires it.
Once you begin diving in to the internals of what makes Rails (and Ruby itself) tick, it becomes clear where these top-level-seeming functions are going and how methods can apparate out of thin air.
For Rails specifically:
- Adam Sanderson has an excellent series on reading the Rails source code, unpacking and explaining the dense code and expanding upon the idioms that get used throughout the codebase.
- The CodeSchool hands-on video course Ruby Bits 1 and Ruby Bits 2 helped a lot in learning how Rails uses metaprogramming patterns to construct a pseudo-DSL.
Learn what’s there
Reading through the libraries (and even just reading through the full API docs) will expose you to a number of utility functions and data structure parameters you might not have known existed. Did you know you could specify the
load factor for a Java hashtable?
Clear your intentional blindspots
A lot of times standard libraries contain the types of data structures and algorithms you seldom encounter when you are programming at the application level. Often times, then, when you step into an interview and they ask you how you might implement a hash table, you panic. Whenever you wanted to use a hash table, you would reach for
Reading the low level constructs of your language will help you prepare for your interviews and learn how some of the more insteresting things in computer science are implemented in the real, road-tested systems.
How do you get started?
Give it a shot in your language of choice!
- Java: GrepCode is a nice viewer for the standard Java libraries
- Python: pure python libraries are available in /Lib (web viewer), and C modules are in /Modules (web viewer). The Django framework is also a good project to read (github), and there’s a great video about the Python dict implementation.
- Ruby: Qwandry is a neat tool for popping open Ruby and Ruby on Rails libraries with just one shell command. You can also read the Ruby source on GitHub. /lib (on GitHub) is a good place to start.
- C++: libstdc++ STL implementation has a number of interesting libraries (web viewer, click on “Go to the source code of this file” to view sources)
- PHP: interpreter code (on GitHub)
- C#: see the Mono implementation (on GitHub) or install ILSpy to read sources of standard API.
What is Coding for Interviews?
Each week, Coding for Interviews members receive two things:
- A programming interview question
- A distilled computer science topic review
You send in your answer and the next week we review solutions.
We practice a little bit each week. The idea is, the next time our group members are looking for jobs, we will be prepared.
New group members are always welcome!
One email each week. No spam. Easy unsubscribe.