Meebo Interview Questions

| No TrackBacks
meebo-logo.pngIn May of 2007 I interviewed with Meebo for a Server-Side Engineer opening.  Part of the initial screening process includes asking the candidate some interesting questions in C/C++ as shown on their job opening site at http://www.meebo.com/jobs/openings/server/.  Ideally the candidate is to "solve" these puzzles and submit their answers with a resume.  Needless to say, I didn't get the job but I did go through a few rounds of interviews.  I found the initial puzzles interesting, and so here are my answers for anyone who's curious ... meebo.zip.

Continue reading for the puzzle questions, or find them here.

1. With these variable declarations: int i, j[10], *k;
Which of the following are legal?

    /*
     * Nothing is wrong with this statement.  It's
     * dereferencing the pointer to j[3] and storing the
     * value in i.  &(j[2])+1 is the address of j[2] plus
     * the size in storage of the object that it is pointing to.
     * In this case, an int (32-bit or 4-byte), so the
     * pointer is incremented by 4-bytes and finally derefrenced
     * using the * to get the actual value at j[3].
     */

    a. i = *(&(j[2]) + 1);

    /*
     * Nothing is wrong with this statement.  It assigns to k, an
     * integer pointer, the memory address of j[1], the second element
     * in j.
     */

    b. k = &(j[1]);

    /*
     * There is a problem with this statement.  First, it's making
     * an assignment to an integer from a pointer without a cast.
     * More importantly, however, is that it's taking the address
     * of j[3] and assigning it to a non-pointer int possibly causing
     * some truncation on 64-bit platforms.  Since int's are typically
     * defined as 32-bit values, and pointers will be 64-bit on a 64-bit
     * box, the address of j[3] will be truncated when the value is
     * assigned to i.  However if you're building a 32-bit binary, it should
     * be OK.
     */

    c. i = &(j[2]) + 1;


2. Assuming the function lookupName is defined, what's wrong with this code (hint: 2 bugs)?

    const char *getName(const char *c) {
        std::string name = lookupName(c);

    /*
     * NULL is traditionally defined as zero so
     * the comparison of checking name == NULL will
     * fail with an error.  There's no match for
     * operator '==' within the string object for
     * name == 0.
     */

        if (name == NULL)
            return "Anonymous";

    /*
     * This can be a problem.  The object "name" is
     * defined within the local scope of getName().
     * When getName returns, the pointer returned from
     * std::string::c_str will no longer be valid. This
     * is because the string object "name" will be
     * destructed when the function returns. It's probably
     * OK in this trivial program, but is definitely not
     * safe in general.
     */

        return name.c_str();
    }

    int main(int argc, char *argv[]) {
        const char *name = NULL, *c = NULL;
        if (argc >= 2)
            c = argv[1];
        name = getName(c);

        printf("My name is %s\n", name);
        return 0;
    }

3. What's wrong with this program? If you were to fix it, what would the intended output be?

    void swap(char *str, int index1, int index2) {
        char tmp = str[index1];
        str[index1] = str[index2];
        str[index2] = tmp;

    }

    int main(int argc, char *argv[]) {
        char *planet1;
        char *planet2;

        planet1 = (char *) malloc(7 * sizeof(char));
        if (!planet1)
            return 0;

        snprintf(planet1, 7, "Jupiter");
        planet2 = "Saturn";

        swap(planet1, 0, 3);
        swap(planet2, 3, 4);

        printf("results: %s and %s\n", planet1, planet2);
        return 0;
    }

Did You Find this Helpful?

Did you find this post helpful, or at least, interesting?

  

About Mark

A Silicon Valley native, Mark Kolich is a full-time Software Engineer and a consultant for hire. A web technologies expert, his current focus is on building powerful and robust cloud-driven web-applications using Java, PHP, Perl, AJAX, DHTML, CSS, and JavaScript. His favorite programming languages are PHP, Java and JavaScript. He uses Linux, enjoys biking to work, loves building great software, and always writes elegant, readable, and maintainable code.

No TrackBacks

No trackbacks attached to this entry.

Twitter (@markkolich)

Translate

About this Entry

This page contains a single entry by Mark Kolich published on October 25, 2008 10:53 AM.

First blog post ... ever was the previous entry in this blog.

Basics of SCSI: Firmware Applications and Beyond is the next entry in this blog.

Find recent content on the main index or look in the archives to find all content.