Question 1

Enter your username.

Question 2

If you want to communicate anything to me, please use this box to do so.

Questions 3-16

These were from a bank, and were differentiated by their hash keys. Each was worth 2 points.
6417f

What is the number of strings of size x where each character of the string can have one of y values? [6417f]


8991f

What does the following program print? [8991f]

int main()
{
  int v1, v2, v3;

  v1 = 0x5cf882
  v2 = 0xff3792
  v3 = v1 ^ v2;
  printf("0x%x\n", v3 ^ v1);
  return 0;
}


Suppose that v.size() equals n and w.size() equals m. What is the big-O running time of the following procedure? [a20a72]

int a20a72(const vector <int> &v, const map <int, int> &w) {
{
  int i, rv;
  map <int, int>::const_iterator wit;

  rv = 0;
  for (i = 0; i < v.size(); i++) {
    wit = w.find(v[i]);
    if (wit != w.end()) rv += wit->second;
  }
  return rv;
}


Let v.size() equal n. What is the running time of the following procedure? [a358df]

int a358df(const vector <int> &v, map <int, int> &p, int m) 
{
  int i;

  p.clear();

  for (i = 0; i < v.size(); i++) p.insert(make_pair(v[i]%m, i));
}


Suppose that v.size() equals n and w.size() equals m. What is the big-O running time of the following procedure? [a5e11]

int a5e11(const map <int, int> &v, const map <int, int> &w)
{
  map <int, int>::const_iterator vit, wit;
  int rv;

  rv = 0;
  for (vit = v.begin(); vit != v.end(); vit++) {
    wit = w.find(vit->first);
    if (wit != w.end()) rv += wit->second;
  }
  return rv;
}


Let v.size() equal n. What is the running time of the following procedure? [ac3f4]

int ac3f4(const vector <int> &v, multimap <int, int> &p) 
{
  int i;

  p.clear();

  for (i = 0; i < v.size(); i++) p.insert(make_pair(v[i], i));
}


Let v.size() equal n. What is the running time of the following procedure? [ac4a7]

int ac4a7(const vector <int> &v, unordered_multimap <int, int> &p) 
{
  int i;

  p.clear();

  for (i = 0; i < v.size(); i++) p.insert(make_pair(v[i], i));
}


Assuming f(i,j) is O(1), what is the big-O running time of the following C++ procedure? [ae639]

int ae639(int n)
{
  int i, j, rv;

  rv = 0;
  for (i = 0; i < n; i++) {
    for (j = 0; j < i; j++) 
      rv += f(i,j);
    }
  }
  return rv;
}


c3d75

What is the big-O running time of performing m Union() operations on an instance of disjoint sets with n elements. The implementation is "Union by rank with path compression." [c3d75]


d171a

What is the big-O running time of performing m Find() operations on an instance of disjoint sets with n elements? The implementation is "Union by rank with path compression." [d171a]


ea518

What does the following program print? [ea518]

int main()
{
  int v1, v2;

  v1 = 0x4a;
  v2 = 0x8c;
  printf("0x%x\n", v1 | v2);
  return 0;
}


eea0c

What does the following program print? [eea0c]

int main()
{
  int v1, v2;

  v1 = 0x4a;
  v2 = 0x7c;
  printf("0x%x\n", v1 & v2);
  return 0;
}


f474a

Suppose that v.size() equals n and l.size() equals m. What is the big-O running time of the following procedure?

void make_list(const vector <int> &v, list <int> &l)
{
  int i;
  list <int>::const_iterator lit;

  l.clear();

  for (i = 0; i < v.size(); i++) {
    for (lit = l.begin(); lit != l.end() && *lit < v[i]; lit++) ;
    l.insert(lit, v[i]);
  }
}


ffc6f

What does the following program print? [ffc6f]

int main()
{
  int v1;

  v1 = 0x4a;
  printf("0x%x\n", v1 << 12);
  return 0;
}


Question 17 - 6 points

f7527

The following is an instance of disjoint sets. We are doing union by size.

Suppose I do Union(2,66). Please answer the following:

A. What is the value in index 2 of the links vector? [A]
B. What is the value in index 66 of the links vector? [B]
C. What is the value in index 2 of the sizes vector? [C]
D. What is the value in index 66 of the sizes vector? [D]


Question 18 - 6 points

76112

The following is an instance of disjoint sets. We are doing union by height.

Suppose I do Union(0,4). Please answer the following:

A. What is the value in index 0 of the links vector? [A]
B. What is the value in index 4 of the links vector? [B]
C. What is the value in index 0 of the heights vector? [C]
D. What is the value in index 4 of the heights vector? [D]


Question 19 - 6 points

8df0f

The following is an instance of disjoint sets. We are doing union by rank with path compression.

Suppose I do Find(10). Please answer the following:

A. What is the value in index 4 of the links vector? [A]
B. What is the value in index 10 of the links vector? [B]
C. What is the value in index 16 of the links vector? [C]
D. What is the value in index 50 of the links vector? [D]
E. What is the value in index 52 of the links vector? [E]
F. What is the value in index 67 of the links vector? [F]


Question 20 - 20 points

Please answer each of the following. Don't calculate it out to be a number, but simply give an expression. Use the carat (^) for exponentiation, exclamation point (!) for factorial, and C(n,k) for n-choose-k.

Part A: You force each of your employees to choose a password composed of exactly 5 lower-case letters followed by 4 single numeric digits. How many potential passwords are there? [---------]

Part B: You are running a lottery. You have a machine with 17 ping-pong balls, numbered 1 to 17. It has been programed to choose one of these balls at random every second. Each time a ball is chosen, it is put back onto the machine, so your machine always has the same 17 balls. Your lottery number is composed of the ping-pong ball numbers chosen in the last 6 seconds, ordered from most recently-chosen to least recently-chosen. How many different lottery numbers are there? [---------]

Part C: You are buying a new home. In your old home, you had 17 different TV's. Your new home only has room for 12 TV's, so you have to donate 5 of them to charity. How many different ways are there for you to donate those 5 TV's to charity? [---------]

Part D: There is a skyscraper in New York City whose spire has 11 lights arranged vertically. You can put a gel over a light, and that makes it a specific color. You have 11 different-colored gels. Each night, you'll use all of the gels, but you want to make sure that the gels form a different arrangement of colors every night. How many nights can you go without ever repeating an arrangement? [---------]

Part E: I'm having breakfast at a bed-and-breakfast, and my host asks me what kind of juice I want. Instead of just saying "Orange", I was dumb enough to ask what she had. She said, "I have 12 kinds of juice. You can have any of them on their own, or you can combine any of them that you want in equal quantities. Or you can skip juice altogether! See, there are infinite combinations!!" You're too polite to tell her that she's wrong. How many combinations are there? (I hate to say that this is a true story, but it is): [---------]


Question 21 - 10 points

You are representing sets composed of numbers from 0 through 30 using integers and bit arithmetic.

Please write the procedure exactly_n(), which has the following prototype:

int exactly_n(const vector <int> &sets, int n);

The return value should be a set of numbers that are in exactly n of the sets in sets. For example, suppose that sets represents the following three sets:

{   { 0, 2, 5, 8 },
    { 0, 3, 9, 11 },
    { 0, 3, 8, 11  }   }
Then:

You cannot use any additional data structures in your implementation. You only need some ints (I used four).


Question 22 - 12 points

Your colleague has written a function called f() with the following prototype:

bool f(long long n);

Your colleague has guaranteed you the following things about f():

  • There is a value x, which is greater than zero but less than 250, such that for all integers n between 0 and x, f(n) is false.
  • For all integers n > x, f(n) is true.

    Write a program that prints out the smallest value n for which f(n) is true. Simply call f() when you want to use it -- your colleague has written that for you.

    Your program should run optimally from a big-O standpoint.


    Question 23 - 12 Points

    You've invented a puzzle that involves a block of wood with 17 holes numbered 0 through 16, and 16 golf tees. You put the tees in all of the holes except for one, and then you play the game as follows: Obviously, you can represent the state of any game, with any number of tees and holes, with a string that contains just '0' and '1' characters. '0' means empty hole, and '1' means that there is a tee in the hole.

    Write a procedure with the following prototype:

    bool can_i_solve(string &s);
    

    The procedure should return true if you can play the game all the way down to just one tee left on the board. It should return false otherwise. Hint -- use recursion like the Sudoku solver.

    Examples:

    String                  Answer
    1                       true -- Any string with just one '1' is solved and "true"
    01                      true
    10                      true
    00010                   true
    11111                   false -- Any string with more than one tee, where you can't jump, is solved.
    110                     true -- One jump will do
    011                     true -- Ditto
    101                     false -- You can't make any jumps.
    1101                    true -- Jump 1 with 0, then 2 with 3.
    11001                   false -- You can jump 1 with 0, but then you're left with 00101.
    

    Remember to set your input box to "preformatted" and resize it so that it's big -- that helps you see what you're writing.