I had an interview yesterday for a Senior Software Developer role. I can’t remember the question very well, but I try to write it as much as I remember.
Question:
Write a function that takes a string as the input and return true if traversed, false if not.
Requirements: (as much as I remember)
- There is only one input type: string.
- The string parameter must be passed by reference.
- No new variables should be declared within the function — he meant that if the parameter is called
$str, then manipulation should only be done on that. He didn’t like me using a second variable to set$reverseStretc. - No looping every character in the string, he specifically said not to loop thru every character in the string.
- No built-in PHP functions could be used; I think he was fine with
strlen().
My Answer: (nope he didn’t like it)
$str = 'this is testing';
$length = strlen($str);
$reverseStr = '';
for($i=$length-1; $i>=0; $i--) {
$reverseStr .= $str[$i];
}
While this does the job somewhat, he didn’t like me to go through every character to get $reverseStr. I am guessing I needed to think about a recursive solution get the last character of the string and index etc., but I am thinking about these as I am writing this, too late!
What you guys thinking?
the question isn’t clear at all.. but if you want to check if the string is a palindrome: