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)
$str
, then manipulation should only be done on that. He didn't like me using a second variable to set $reverseStr
etc.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:
function is_palindrome ($str){
if(strlen($str)==0){return true;}
if($str[0]==$str[strlen($str)-1]){
return true and is_palindrome(substr($str, 1, strlen($str)-2));
}else{
return false;
}
}