C++ answers to 'cracking the coding interview', unit tested, containers written from scratch. Feedback please so I can iterate. [github]

Yeah. Or:

def anagram(a, b): return sorted(a) == sorted(b)

and

def replace_space(a): return re.sub(" ", "%20", a)

in python :) Sure that's even better than writing it in C++ right? And look! It's shorter too! Did I get the job? Did I get the job?

Question 1.1:

Implement an algorithm to determine if a string has all unique characters. What if you cannot use additional data structures?

Question 1.2:

Implement a function void reverse(char* str) in C or C++ which reverses a null-terminated string.


My issues with your answers:

Question 1.1:

  • Can your first answer be considered to be an algorithm? Given the context, to me an algorithm means writing the code that performs the logical operations on somewhat atomic pieces of data. Not calling a library function that does all the work for you. yes technical interviews are mean and stupid. But they're not my creation, so please kindly don't shoot the messenger :). Maybe some interviewers consider calling a library function instead of attempting to write an 'algorithm' as requested to be taking the piss? The answer in the book certainly suggests the solution I've posted. In fact they look very similar. I've been given a bit of power today as your interviewer, and I'll use every bit of it I can.. did not read the question properly: no offer.

Question 1.2:

  • where is your void reverse(char* str) function? To me this looks like "did not read the question properly": no offer.

Solution 1.1 offered in the Book:

package Question1_1;
public class Question {
public static boolean isUniqueChars(String str) {
    if (str.length() > 128) {
        return false;
    }
    int checker = 0;
    for (int i = 0; i < str.length(); i++) {
        int val = str.charAt(i) - 'a';
        if ((checker & (1 << val)) > 0) return false;
        checker |= (1 << val);
    }
    return true;
}

public static boolean isUniqueChars2(String str) {
    if (str.length() > 128) {
        return false;
    }
    boolean[] char_set = new boolean[128];
    for (int i = 0; i < str.length(); i++) {
        int val = str.charAt(i);
        if (char_set[val]) return false;
        char_set[val] = true;
    }
    return true;
}

public static void main(String[] args) {
    String[] words = {"abcde", "hello", "apple", "kite", "padle"};
    for (String word : words) {
        System.out.println(word + ": " + isUniqueChars(word) + " " + isUniqueChars2(word));
    }
}

}


I think we're just highlighting how profoundly stupid technical interview questions are... I agree, I agree, I agree. It's stupid. :)

May the flamewar begin. :D

/r/programming Thread Parent Link - github.com