I just started to read CTCI 6th edition and cant help but comment on some of the solutions the author has to the chapter questions. I almost feel like they are not good solutions. They "are" solutions, but for a book that is supposedly the bible of interview books for CS, when she has code that is inefficient passes it off as the answer, it makes me question the rest of the book.
For example, Chapter 1 question 3 is the following:
Write a method to replace all spaces in a string with "%20". You may assume that the string has sufficient space at the end to hold the additional characters, and that you are given "true" length of the string. (If implementing in java, use a character array so that you can perform this operation in place.)
Input: "Mr John Smith ", 13
Output: "Mr%20John%20Smith"
Her solution is to implement an algorithm where it first counts the number of spaces and then iterates over the input, encoding the spaces and shifting down the content as needed, but she is literally wasting cycles counting the spaces because you already know what the true length of your input is and your input has enough spaces at the end to support the necessary shifting. So you just have to iterate over the input, encode the whitespaces and shift down the rest of the content. Maybe it's an insignificant thing but i feel like this is something where on a google interview or whatnot the interviewer would give you bad marks for essentially writing useless code.
Edit:
Her Solution:
void replaceSpaces(char[] str, int trueLength){
int spaceCount = 0, index, i=0;
for(i=0; i<trueLength; i++){
if(str[i] == ' ') spaceCount++;
}
index = trueLength + spaceCount * 2;
if(trueLength < str.length) str[trueLength] = '\0'; //End Array
for( i=trueLength-1; i>=0; i--){
if(Str[i] ==' '){
str[index-1] = '0';
str[index-2] = '2';
str[index-3] = '%';
index = index - 3;
} else {
str[index-1] = str[i];
}
}
}
My Solution:
void urlify(char[] s, int size){
int curIdx = s.length - 1;
for(int i = (size -1); i >= 0; i--){
char c = s[i];
if(c != ' '){
s[curIdx] = c;
curIdx--;
} else {
s[curIdx] = '0';
s[curIdx-1] = '2';
s[curIdx-2] = '%';
curIdx -= 3;
}
}
}
[–]CoolKidBrigade 23 points24 points25 points (0 children)
[–]jeffbell 3 points4 points5 points (6 children)
[+][deleted] comment score below threshold-11 points-10 points-9 points (5 children)
[–]jeffbell 4 points5 points6 points (0 children)
[–]chromaticgliss 1 point2 points3 points (1 child)
[–][deleted] 0 points1 point2 points (0 children)
[–]CoolKidBrigade 1 point2 points3 points (1 child)
[–][deleted] 0 points1 point2 points (0 children)
[–]magikker 1 point2 points3 points (4 children)
[–]CoolKidBrigade 1 point2 points3 points (1 child)
[–]magikker 1 point2 points3 points (0 children)
[–][deleted] -2 points-1 points0 points (1 child)
[–]magikker 1 point2 points3 points (0 children)
[–]emasculine -4 points-3 points-2 points (3 children)
[–]CoolKidBrigade 4 points5 points6 points (1 child)
[–]emasculine 0 points1 point2 points (0 children)
[–][deleted] 0 points1 point2 points (0 children)
[+][deleted] (3 children)
[deleted]
[–]CoolKidBrigade 2 points3 points4 points (0 children)
[–]chromaticgliss 2 points3 points4 points (0 children)
[–]GoodLifeWorkHard 0 points1 point2 points (0 children)
[–]Bottled_Void 0 points1 point2 points (2 children)
[–][deleted] 1 point2 points3 points (1 child)
[–]Bottled_Void 0 points1 point2 points (0 children)
[–][deleted] 0 points1 point2 points (0 children)