GeeksforGeeks » Interview Questions

Google Interview Question for Software Engineer/Developer about Algorithms

(4 posts)
  1. abhi
    guest
    Posted 1 year ago #

    write code for a recursive solution that you created, can be any problem.

  2. kartik
    Moderator
    Posted 1 year ago #

    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;
    }
    
  3. m@}{
    guest
    Posted 1 year ago #

    "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.

  4. m@}{
    guest
    Posted 1 year ago #

    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.

RSS feed for this topic