Technical Easy Interview Questions

Suppose you have a database with two tables, employee and department (see below for exact schema). Write a SQL statement to answer the following questions:

  1. Show a list of all employees that includes both the employee name and the department name.
  2. Show a list of departments and include the number of employees in each department.
  3. Show a list of all departments with no employees.
  4. Show a list of departments with more than 2 employees
  5. Show a list of departments that includes average and maximum salary in that department. Sort the list by the average salary.
Employee Table
---------------------
id
first_name
last_name
salary
department

Department Table
-----------------------
id
name

Using browser default CSS, hyperlinks are displayed with a blue underline. Suppose you are writing a website and instead, you wish the default to be bold red font, with no underline. What would you put in your stylesheet?

Now suppose that when the user hovers their cursor above the hyperlink you want the text to turn green. What would you add to your stylesheet?

Suppose you have two CSS rule sets defined .bold {font-weight: bold; } and .bordered {border: 2px solid #CCC;} Write an additional rule-set that selects elements that are both .bold and .bordered and changes the font color to red?

Suppose you have the following array

stuff = [:dog,:cat,:orange,:banana]
  1. How can you slice this array to create a new array [:cat,:orange]
  2. Add the element :apple on to the end of the array.
  3. Now take :apple back off again
  4. Add the element :fish to the start of the array.
  5. Now remove the element :fish.

RailsOnRails is unique in its ability to create a complete, working web application in approximately 5 minutes. A solid RoR engineer should be able to demonstrate this.

Create a new RoR application with two models: Blog and Comment. Blog should have two string properties: title and body. Comment has one string property: body. A Comment belongs to a Blog.

Configure routes such that Blogs are shown from path /blogs/:id and the comments for a particular blog are created at /blogs/:blog_id/comments/new and viewed at /blogs/:blog_id/comments.

Explain the difference between the following quoting in Python:

my_string = 'the cat sat on the mat'
my_string = "the cat sat on the mat"
my_string = """the cat sat on the mat"""
my_string = '''the cat sat on the mat'''

Explore the basics of the display attribute:

  1. What are the possible values for the DISPLAY attribute?
  2. What's the default value?
  3. What the difference between display:none and visibility:hidden?

Explore the basics of the position attribute:

  1. What are the possible values for the POSITION attribute?
  2. What's the default value?
  3. How does the browser decide where to position absolute elements?

Given the following C code:

struct department {
 char name[20];
 int id;
};

struct department d;

add a typedef to allow easy declaration of departments and add some code that uses the typedef.

Explain what the following code does:

#include <stdio.h>

int main()
{
   int i;
   int *p;

   i = 2;
   p = &i;

   *p = 3;      

   printf("%d\n", i);

   return 0;
}

What will original and copy evaluate to in each of the following examples? Explain why there is a difference.

original = "hello"
copy = original
copy << " there"
puts copy
puts original
original = "hello"
copy = original
copy += " there"
puts copy
puts original

In each example below indicate whether or not we should be asserting true or false. (E.g. what should the ? be?).

Explain the difference between ==, eql and equal?

str1 = "chitter"
str2 = "chitter"

assert_equal ?, str1 == str2
assert_equal ?, str1.eql?(str2)
assert_equal ?, str1.equal?(str2)
assert_equal ?, str1.object_id == str2.object_id
sym1 = :chatter
sym2 = :chatter

assert_equal ?, sym1 == sym2
assert_equal ?, sym1.eql?(sym2)
assert_equal ?, sym1.equal?(sym2)
assert_equal ?, sym1.object_id == sym2.object_id

Create a new RoR application with two models: Blog and Comment. Blog should have two string properties: title and body. Comment has one string property: body. A Comment belongs to a Blog.

The route /blog/:id should show the title and body properties of the blog. Below the body of the blog, add a link that says "Show Comments". When this link is clicked, load and display the comments for the blog via AJAX.

Suppose you have a Rails User model as follows:

class User < ActiveRecord::Base
   # Properties of this model are string:name and boolean:is_admin
end

The is_admin property is used to denote whether or not a user should be granted administrative privileges. Only users who are admins should be able to grant admin privileges to users.

Suppose you expose a form in your application in which any user may edit their name. For example

<%= form_for @user %>
   <%= f.label :given_name %>  
   <%= f.text_field :given_name %>
   <%= f.submit "Update"  %>
<% end %>

Explain how you've just opened up a security hole in your application. How can you fix this?

Write a helper method that takes a DateTime object date as it's single parameter.

  1. If the date is less then two days ago, show the distance of time in words. (e.g. "about 4 hours ago").
  2. If the date is more then two days ago, show the actual date. (e.g. Jan 12, 2012)

E.g. fill in the guts of the following method

def distance_of_time_in_words_or_absolute(date)
   # Your code here
end

The following code will fail with NoMethodError : private method 'dream' called for Dog

Assuming that you cannot modify the source code for the Dog class and that the method
must remain private - how can you nonetheless call this method from outside of Dog class?

class Dog
    def speak
        puts "woof"
    end

    private

    def dream
        puts "chasing a rabbit"
    end
end

dog = Dog.new
dog.speak
dog.dream

Print out the prime numbers between 1 and 100. As a first pass, don't worry about writing an efficient algorithm. Just write clear code that is easy to follow. Once you've done that, consider different possible optimizations.

Write a program to print out a multiplication table, from 1x1 to 12x12. This should look like:

   1   2   3   4   5   6   7   8   9  10  11  12
   2   4   6   8  10  12  14  16  18  20  22  24
   3   6   9  12  15  18  21  24  27  30  33  36
   4   8  12  16  20  24  28  32  36  40  44  48
   5  10  15  20  25  30  35  40  45  50  55  60
   6  12  18  24  30  36  42  48  54  60  66  72
   7  14  21  28  35  42  49  56  63  70  77  84
   8  16  24  32  40  48  56  64  72  80  88  96
   9  18  27  36  45  54  63  72  81  90  99 108
  10  20  30  40  50  60  70  80  90 100 110 120
  11  22  33  44  55  66  77  88  99 110 121 132
  12  24  36  48  60  72  84  96 108 120 132 144

Explain views in Drupal. When would you use them? What are they good for? When should they be avoided?

What are the most important modules to consider when creating a website? What are common modules that you'd avoid? Why?

What is a node in Drupal? Give an example of different types of nodes that you might create? How can you fully customize the functionality for creating and displaying a node? How can you extend the fields available in a node?

What is a signal in Django? Where might you use one? What's the difference between send() and send_robust()? When might you use one over the other?

What does the following code do? What's another way of doing the same thing?

from django.core.signals import request_finished
from django.dispatch import receiver

@receiver(request_finished)
def my_callback(sender, **kwargs):
    print "Request finished!"

You have a closet with 300 shirts in it. Consequently, you find it hard to find a shirt quickly while dressing for work in the morning.

What can you do to organize your shirts to minimize the time that it takes to find the correct one?

Write a function that takes as input a sorted array and modifies the array to compact it, removing duplicates. Also return the new length of the array.

Notes: The input array might be very large.

For example:

  • input array = [1, 3, 7, 7, 8, 9, 9, 9, 10]
  • transformed array = [1, 3, 7, 8, 9, 10]
  • size = 6

Imagine a bear walks the following paths:

  1. South one mile
  2. Left turn
  3. East one mile
  4. Left turn
  5. North one mile

The bear arrives at where he started. What color is the bear? Why?

Consider a Rails application with the following two models: Book and Video. Book has properties title and summary. Video has properties title and studio. Suppose that when both of these models are displayed in a the application, a user may add a Comment (with single property body). (E.g. both Books and Videos have many Comments).

Create the appropriate migrations and models (including relationships).

Now, suppose on your home page you want to show a list of all recent Comments, sorted by created_at. The list should show the body of the Comment as well as the type and title of the object about which the comment was made.

Write the ActiveRecord query necessary to display this list of recent Comments. Explain what the N + 1 query problem is, and how you've avoided it in your solution.

Suppose you have the following set of models and relationships

class Author < ActiveRecord::Base
   has_many :blogs
   # Properties:  name
end

class Blog << ActiveRecord::Base
  belongs_to :author
  has_one :image
  # Properties:  title, body
end

class Image << ActiveRecord::Base
  belongs_to :blog
  # Properties:  image_file_path
end

Suppose you want to create a table that shows all Blogs. The columns of the table are the blog title, the author name and the blog image.

Write an ActiveRecord query that will pull the list of @blogs necessary to display this table. Can you do this such that under the covers no more then 3 SQL statements are generated? How about no more then 1?

Create a Rails application with two models: User (properties: string:name) and Address (properties: string:street, string:city). A User has one Address.

Create a view / controller in which a new User and their Address may be created in a single form. Make all properties for both models are required in the form.

Suppose you have a User model with properties string:name, integer:age, string:occupation.

Write validations on the model such that

  1. Name is required
  2. Age is optional, but must be numeric, and not less then 5
  3. Occupation is required if the age is greater then 21

Suppose you have two models User and Address. A User has at most one address. User has property datetime:last_login_at. Address has properties string:city and string:state.

Write an ActiveRecord query to return an array of Users who live in California, ordered by the last_login_at.

Write an ActiveRecord query to return an array of Users who live in California or have no listed address.

Describe the difference between a class instance variable and a class variable in Ruby. Which bears more resemblance to a static field in Java?

Given the following HTML:

<div class=”article”>
<h1>Buddy In-law</h1>
<p>A friend of a friend of yours. <a href=”https://www.google.com/?q=someone">Someone</a> you have yet to meet, but is friends with your friend. Someone you share a mutual friend with. Someone you share a mutual buddy with.</p>
</div>

How would you:
1. Change the paragraph font to Verdana?
2. Change the font size of the heading to a 20px
3. Change the font color of all anchors in article paragraphs to pink. Change the hover state color to red
4. Justify the paragraph text
5. Make the whole article 600px wide and center the heading