GeeksforGeeks » Interview Questions
Google Interview Question for Software Engineer/Developer about Algorithms
(4 posts)-
write code for a recursive solution that you created, can be any problem.
-
Print numbers from 1 to n :)
#include<stdio.h> void print(int n) { if(n == 0) return; print(n-1); printf("%d ", n); } int main() { print(30); getchar(); return 0; } -
"can be any problem"
1. Stack overflow may occur, if the recursion is too deep.
2. There is recursion depth restriction for some languages ( for example python, sys.setrecursionlimit(2000) )
3. Tail recursion doesn't converted to iterative process by most compilers and interpreters. -
void print( int n ){ if( n == 0 ){ return; } print(n - 1); System.out.print( n + ", "); }If we call 'print(7000)' function in java the following exception will occur:
Exception in thread "main" java.lang.StackOverflowError
Reply
You must log in to post.