kellegous.com

 

Article

Geek Quiz

April 22nd 2004 in the wee hours

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) {
    …
}

trackbacks / pings

Pings are currently disabled; they will be accepted very soon.

comments

#1
kellegous
anyone? does nobody else like puzzles?
#2
El Noodle
Senor Kellegous,

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);
}
#3
kellegous
excellent. here's what i came up with. unfortunatley, it took me longer than would have been acceptable in an interview.

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);
}

Post a Comment: To leave a new comment, simply type your message below. Markup is also allowed as long as it conforms to XHTML Strict. A list of allowable tags is available in the Comment Guidelines. Obviously, if the words “XHTML Strict” mean nothing to you, you should stick with just typing your message below.

about kellegous.com

kellegous.com is the personal site of kelly norton, a designer and engineer living in Atlanta, Georgia. Kelly used to be a graduate student at the MIT Media Lab but graduated in the summer of 2006. Before that, he was the Senior VP of Technology Development for Connexxia, a small technology company in Atlanta. He now works as a Software Engineer for Google. (more…)

now reading



syndicated feeds


 physical language workshop attribution-sharealike license / / xhtml / css