How To Solve This Apple Coding Interview Question With Recursion

preview_player
Показать описание
Solution to a very popular coding interview question asked recently by Apple.

#softwareengineering #javascript #shorts
Рекомендации по теме
Комментарии
Автор

Splitting a return statement over 2 lines is a war crime

rochie
Автор

The compiler "So what you say you want is a for-loop"

AndrewTSq
Автор

Bonus question. About how large would the string need to be in order for this solution to produce a stack overflow error

evanfreethy
Автор

Oh boy the overhead.
Thats a great example for when NOT to use recursion.

MrAntiKnowledge
Автор

Prefer doing it using the first item

def rev(s: str):
if len(s) == 1:
return s[0]

return rev(s[1:]) + s[0]

ashiqurrahman-nuik
Автор

Dude thanks for these shorts!! I enjoy watching them just as funny food delivery shorts (yes, take this as a compliment)

malikau
Автор

I'm c++ programmer and i wonder - why recursion when basic loop would be the same with better memory complexy, even better you can just loop over half of string and just swap chars (also in cpp you can use reverse iterator)

qwertioxgame
Автор

I did:

If (str.length < 2) return str;
return str.charAt(str.length -1) + solution(str.substring(1, str.length -1)) + str.charAt(0)

hajii
Автор

Slicing the string for every character seems like an overkill.

1. Add index to track the current character
2. Start from the last - solution(str, str.length - 1)
3. If i == 0 return str[0]
4. return str[i] + solution(str, i - 1)

romazherdev
Автор

Getting better. I solved this one in my head

cabbage-dev
Автор

I thought the whole purpose of these coding interviews were to test your algorithms and data structures skills and using slice() would defeat that purpose.

jeffreymarte
Автор

Recursion is cool, but you have to take care of stack size.

robertochostakovis
Автор

I'd try to do something like

reverse(&text, index1, index2)
If index1 > index2 return ""

Swap(text[index1], text[index2])
Return reverse(text, ++index1, --index2)

This may swap the middle char with itself when the string is of uneven length.

Yeah, but depending on language a simple,

txt = txt[-1:0] could do it without recursion

Cyrrex
Автор

function solution(str) {
if(str.length <= 1) return str
return solution(str.substring(1)) + str.charAt(0)
}

nanonkay
Автор

function reverseString(string) {
if(string.length === 1) return string
return string.slice(-1) + reverseString(string.slice(0, -1))
}

noobdevtutorials
Автор

being a haskeller, recursion is the only way available for reversing a string:

reverse :: String -> String
reverse = go []
where
go acc [] = acc
go acc (x:xs) = go (x:acc) xs

asdfghyter
Автор

arr = str.split("")
while arr.length > 0
revStr += arr.pop()
return revStr

YT-Gmailer
Автор

at that point you might as well return

Lawrence
Автор

Recursion is illegal in NASA programming.


The reason, it's prone to error/unpredictable outcomes for anything slightly more complex than this.

Sensenwerk
Автор

function reverse(str, result = '') {
if (str.length === 0) return result

const [head, ...tail] = str

return reverse(tail, head + result)

}

JacobChristiansen
join shbcf.ru