Skip to content

Instantly share code, notes, and snippets.

@bcjordan
Last active January 2, 2016 03:18
Show Gist options
  • Save bcjordan/8242593 to your computer and use it in GitHub Desktop.
Save bcjordan/8242593 to your computer and use it in GitHub Desktop.

Reading the Standard Libraries

A Coding for Interviews weekly practice problem group member mentioned during our Skype customer interview that reading through the Java collections library was the most valuable step he took while preparing for his Google 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 root defined?
  • What makes the use_doorkeeper code's call to controller different than another?
  • Why and how does User.find_by_favorite_food(:pizza) have a variable name embedded inside the name of the method?

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)
  • 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)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment