CodeSOD: Reversing the String, Belaboring the Point
The position had sat open for months now; the department was straining under the load of too many projects and too few developers, but the pool of candidates was rapidly shrinking. So when Cindy found a resume that looked halfway decent, she immediately recommended tossing them a programming test and scheduling an interview.
The phone screen is a bit superfluous given fifteen years experience, she thought. We'll just use a quick test and get to the good part.
The test was simple enough: reverse a string, in your language of choice. They were hiring iOS developers, so the candidate was wise enough to choose Objective-C- usually a great choice to demonstrate that you won't need much training on the job.
However, generally, you ought to actually be good at the language in question...
-(NSString*)reverseString:(NSString*)originalString{const char *cVersionOfOriginalString = [originalString cStringUsingEncoding:NSUTF8StringEncoding];char *cVersionOfReversedString = malloc((originalString.length + 1) * sizeof(char)); cVersionOfReversedString = &cVersionOfReversedString[originalString.length];*cVersionOfReversedString = '\0';cVersionOfReversedString--; char *simpleChar = (char *)&cVersionOfOriginalString[0];while (*simpleChar != '\0'){*cVersionOfReversedString = *simpleChar;simpleChar++;cVersionOfReversedString--;}NSString *reversedString = [NSString stringWithCString:(cVersionOfReversedString + 1) encoding:NSUTF8StringEncoding];return reversedString;}
Creating a pointer to the last byte of a c-string and walking backwards, depositing characters from the original string as you go, may win some points for creativity, but it's definitely not code you want to see in your iPad app. With a heavy heart, Cindy emailed HR to reject the application. Maybe the guy who barely spoke English deserves another chance...
[Advertisement] Release!is a light card game about software and the people who make it. Play with 2-5 people, or up to 10 with two copies - only $9.95 shipped!