Article
Geek Quiz
Here’s some geeky fun for everyone. I was reading some random blog entry at some random url the other day. There someone was describing a question they had been asked at a job interview. A solution did not immediately come to mind, so I figured I was lucky to have never been asked a similar question. I didn’t linger long at the site, but the question would not go away. Finally when I caught myself staring at the tea pot searching for an easy solution long after the tea was ready, I decided to take out a piece of paper and exorcise my curiosity. I reluctantly admit that it took me longer than would have been acceptable to reach a solution that I liked, but it was a fun puzzle. And most importantly, I can move on with my day. But before I do, I wanted to pos[te] the question here for the annoyance … er … enjoyment of others.
The challenge:
Write a function that takes in two buffers that are NULL padded. The function should swap and reverse the data in the two buffers. Assume the two buffers are equal in length (not the data, of course). Here is an example.
| before | ||||||
|---|---|---|---|---|---|---|
| buffer #1: | h | i | \0 | \0 | \0 | \0 |
| buffer #2: | w | o | r | l | d | \0 |
| after | ||||||
|---|---|---|---|---|---|---|
| buffer #1: | d | l | r | o | w | \0 |
| buffer #2: | i | h | \0 | \0 | \0 | \0 |
one might start something like:
void reverse_swap(char* buffer0,char* buffer1) {
…
}


Ok, so after a few drinky drinks late saturday night, I see your big challenge, and sez to myself "aye, so meester MIT-boy things he so smaht, going to college and whatnot". So I broke out the commodore 64 and started working on a grand solution to your problem. The result was beautiful. In addition to reversing your text, it printed each letter in a different color (well, up to the first 16 letters, and then the colors started over). Unfortunately, I did not save the program, and ultimately my desire to play pac man overwhelmed me and I had to reset the machine.
Today I rewrite program with all enthusiasm. I could not get my 2.2GHz machine to print nice colors though, so I say screw it, and do broot force approach.
This one does it dumb like Ed Asner. It flips each array in place, and then just swaps them. Tripple the memory access, tripple the fun, 1/16th the color spectacle. But is flips it and reverses it like it was Missy Elliot (BWOOP BWOOP!). Amy at least thinks I'm funny.
I post super-duper code in next comment, as preview ate all my returns.
El Code-o:
#include <stdio.h>
char b0[] = "ooj\0\0\0";
char b1[] = "nwo I\0";
#define MAX(a,b) (((a)>(b)) ? (a) : (b))
void swap(char *x, char *y){
char tmp=*x;
*x=*y;*y=tmp;
}
int flip(char *x){
int len, i;
len=strlen(x);
for(i=0;i<len/2;i++)
swap(&x[i], &x[len-i-1]);
return len;
}
void reverse_swap(char *b0, char *b1){
int b0_end, b1_end;
int b0_len, b1_len;
int i,j;
int last;
char t;
char *big,*small;
printf("Original 1:%s\nOriginal 2:%s\n",b0,b1);
b0_len=flip(b0);
b1_len=flip(b1);
printf("Flipped 1:%s\nFlipped 2:%s\n",b0,b1);
for(i=0;i<MAX(b0_len,b1_len); i++){
swap(&b0[i], &b1[i]);
}
printf("Swapped 1:%s\nSwapped 2:%s\n",b0,b1);
}
int main(){
reverse_swap(b0,b1);
exit(1);
}
void revwap(char* buffer0, char* buffer1) {
t,i,j;
char c;
char* s;
for (t=0;(buffer0[t] | buffer1[t]) != 0;t++)
if ((buffer0[t]=='\0') && (buffer1[t]!='\0')) {
s=buffer0;
buffer0=buffer1;
buffer1=s;
}
j=0;
for (i=0;i<t;i++) {
c=buffer1[t-i-1];
buffer1[t-i-1]=buffer0[i];
if (c!='\0')
buffer0[j++]=c;
}
memset(buffer0+t-i+j,'\0',j);
}