Technical Language Features Interview Questions

Write a Math module to extend the Fixnum class. This module should provide two methods. First, a class method called pi that prints out the value of pi to two significant digits. Second, an instance method called squared that squares a number. E.g. when you are finished the following should be possible:

puts Fixnum.pi
# 3.14
puts 2.squared
# 4

Suppose you have a class that performs several expensive calculations. However, during the lifetime of an object, the result of the expensive calculation won't change. Therefore, you wish to ensure that each calculation is performed only once, and that result is cached. A simple technique for this would be as follows:

class Calculator
    def expensive_calc_one
        return @result1 unless @result1.nil?
       @result1 = # Do very expensive calculation.
    end

   def expensive_calc_two
        return @result2 unless @result2.nil?
       @result2 = # Do very expensive calculation.
    end
end

If this class contained many such expensive calculations, this memoization technique would become repetitive. Can you come up with a framework that reduces this repetition by allowing one to simply mark a method as memoized and no longer have to worry about manually handling the caching?

Bonus points: There is a flaw in the simple technique. Under one set of circumstances the caching will fail and repeated calls to expensive_calc_one will result in the expensive calculation being executed again and gain. When would this occur?

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?

You have a class Parent with a virtual method a and a class Child that implements a. You also have a several functions:

void f1(Parent p) { p.a(); }
void f2(Parent* p) { p->a(); }
void f3(Parent& p) { p.a(); }

When called with a Child instance, which method will be invoked for f1, f2 and f3?

Explain in detail what the following C++ code does:

#include <iostream>
using namespace std;

template <class T>
T GetMax (T a, T b) {
  T result;
  result = (a>b)? a : b;
  return (result);
}

int main () {
  int i=5, j=6, k;
  long l=10, m=5, n;
  k=GetMax<int>(i,j);
  n=GetMax<long>(l,m);
  cout << k << endl;
  cout << n << endl;
  return 0;
}

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.

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

Define the following object oriented concepts:

  • Class, object (and the difference between the two)
  • Instantiation
  • Method (as opposed to, say, a C function)
  • Static methods and classes
  • Destructor/finalizer
  • Inheritance
  • Encapsulation
  • Multiple inheritance (and give an example)
  • Abstract class
  • Interface/protocol (and different from abstract class)
  • Method overriding
  • Method overloading (and difference from overriding)
  • Polymorphism (without resorting to examples)
  • Method visibility (e.g. public/private/other)