Technical Moderate Interview Questions

Suppose that generating the home page of your application is very expensive. The content on the page is the same for every user (whether authenticated or not) except for the top right hand corner of the page.

If the user is not authenticated, the top right-hand corner of the page displays a "Login" link.

If the user is authenticated, the top right-hand corner of the page displays "Hello, USER_NAME" and a "Logout" link.

What caching strategy would you employ in this situation?

When opening a connection to a remote service, it's typically important to make sure that the connection (or "resource") is closed - no matter what happens in the code that uses the resource. In Java this might be done as follows:

Connection conn;
try{
  conn = new Connection("some service");
  conn.doSomething();
}
catch(Exception e){}
finally{
  conn.close();
}

This pattern is tedious and repetitious, and depends on the programmer to remember to include the finally clause. In Ruby, we can do better. Suppose we wished developers to be able to safely manage connection resources using a pattern like the following...

Connection conn = new Connection("some server")
using(conn){
   conn.doSomething();
}

Define a Kernel method using that ensures that no matter what happens within the using block, the connection is always closed.

Suppose you have an application with two models, User and Places. A User has many Places and a place denotes a location in the world that a user has visited. Place has two properties string:city and string:country, and User has property string:name.

Somewhere in your site you want to display a list of Users and the most recent Place they have visited. Write an ActiveRecord query to do this.

Now, suppose you have many many Users and Places and to make your SQL more efficient you wish to denormalize your data and add a field int:most_recent_place_id to the User model. How would you go about keeping this field up to date? Write the simplified ActiveRecord query that to display the list of Users and Places.

In Ruby, there are three methods for converting a block into a Proc object (see below). Discuss the differences between these three choices.

b1 = Proc.new{|x| x + 1}
b2 = lamba{|x| x + 1}
b3 = proc{|x| x + 1 }