Saturday, March 1, 2008

CoreOS Interviews

I will describe some questions that I was asked.

1.) Given a list of negative and positive integers, group all the negative integers together and the positive on the opposite side, in place.

int pos = 0;
for (int i = 0; i < n; ++i) {
if (array[i] < 0) {
swap(&array[pos++], &array[i]);
}
}


2.) Implement strstr() with no libraries.

char *strstr(char *hay, char *need) {
if (hay == (char *)0 || char == (char *)0)
return (char *)0;
int haysz = my_strlen(hay);
int needsz = my_strlen(need);
int j = 0;
for (int i = 0; i < haysz - needsz; ++i) {
for (j = 0; j < needsz; ++j)
if (hay[i + j] != need[j])
break;
if (j == needsz)
return &hay[i];
}
return (char *)0;
}


3.) Given 3 buckets labeled B, W, and BW. The labels are wrong, choose a ball out of a bucket to correctly fix the labels. Which bucket must you choose from?
BW, because this will open up the possibilities by process of elimination.
For example, if the ball you choose from BW is white, then that bucket must be W, since it cannot be BW. Moving along, you look at the bucket labeled B, the only alternatives are W or BW. W was already chosen, so the bucket must be labeled BW. Looking at the last bucket labeled W, the only option is B, which satisfies the rules. (This logic works for the ball chosen from the BW bucket is black)

4.) Test the cp command.
Input, type of files, restrictions on non-valid files (i.e., they don't exist).

5.) Find the top six (6) frequencies from 1 - 100, that have the highest strength. You are given a function called int getStrength(int freq); and it will return a value from 1 - 10. 10 is the highest strength.

typedef pair FreqStr;
vector list;
int cmp(FreqStr first, FreqStr next) {
return first.second >= next.second;
}
for (int i = 1; i <= 100; ++i) {
list.push_back(make_pair(i, getStrength(i)));
}
sort(list.begin(), list.end(), cmp);
for (int i = 0; i < 6 && i < list.size(); ++i)
top6[i] = list[i].second;


6.) Given a nxn grid, and '*' represents mine's as in minesweeper. Return the grid with '*' characters remaining, but the blank spaces with numbers, indicating how many mine's are adjacent to that (x,y) position. Adjacent, in this context, means horizontal, vertical, and diagonal from the (x,y) position.

bool inBounds(int x, int y) {
return (0 <= x && x < n && 0 <= y && y < n);
}
int count(int x, int y) {
int cnt = 0;
for (int xoff = -1; xoff <= 1; ++xoff)
for (int yoff = -1; yoff <= 1; ++yoff)
if (inBounds(x + xoff, y + yoff)) {
if (grid[x + xoff, y + yoff] == '*')
++cnt;
}
return cnt;
}
for (int x = 0; x < n; ++x)
for (int y = 0; y < n; ++y) {
if (grid[x, y] != '*') {
grid[x, y] = '0' + count(x,y);
}
}