Read your Damn Standard Libraries

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.

Reading the rubygems source

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 root defined?
  • 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 food embedded 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:

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 HashMap<K,V>.

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:

  1. A programming interview question
  2. 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.

Email

Brian Jordan is a software engineer and game developer in San Francisco, California. Formerly at PopCap Games, TripAdvisor and One Laptop per Child. Brian is the curator of Coding for Interviews, the weekly programming practice newsletter and teacher of the Coding for Interviews course.

Posted in Preparation
  • http://ahmetalpbalkan.com/ Ahmet Alp Balkan

    for .NET, you can refer to Mono implementation https://github.com/mono/mono/tree/master/mcs/class/corlib/System or install ILSpy to read sources of standard API.

    • Brian Jordan

      Great tip, added to post. Thanks Ahmet!

  • adamsanderson

    Hey thanks for mentioning my Rails series Brian. I can’t emphasize enough how important this is for developers, new and old alike.

    Being able to explain how your libraries and frameworks work is always big plus in my book when interviewing candidates.

    If anyone is interested, I wrote a similar series about the Ruby Standard Library on my old blog: http://endofline.wordpress.com/?s=Standard+Library

    • Brian Jordan

      It’s really a great series! Hadn’t seen the standard library version, thanks for the pointer.

  • http://abhinavk.me/ Abhinav Kumar

    Unregistered Sublime Text? :|

    • Brian Jordan

      Hah! I suppose I’m done evaluating. :)

      Fixed!

  • http://qpleple.com/ Quentin Pleplé

    How about PHP?

  • http://bkvirendra.github.com/ Virendra

    You can include the Django source code on Github https://github.com/django/django for Python.

    A great place to learn, how Django really works deep down.

    • Brian Jordan

      Great pointer, will add to article. Thanks Virendra!