Home Ā» Interview Questions Ā» Interviews Ā» Story Details
Printable Version

A Question A Day: Understanding Recursion

by Kamal Rawat on Sep 13, 2011

What is recursion? What is head-recursion and tail-recursion? Should we use recursive or non-recursive functions?
Comments: 0    Views: 1626

MCN Professionals | Interview Question of the day

Date: 12th Sept; 2011

Difficulty Level: ****  

Category: Programming

To receive one interview question in your mailbox daily, please subscribe to "A Question A Day" at MCN Professionals home page.

 

Today's Question: 

What is recursion? What is head-recursion and tail-recursion? Should we use recursive or non-recursive functions?

Answer:

In programming, Recursion is way of designing an Algorithm where a function is defined in terms of itself. Typically the function performs some part of the task and rest is defined in terms of itself.

For example: Sum of first n numbers (n being positive) can be defined in terms of Sum of first (n-1) numbers as below.

recur.jpg

Few points to note in the above recursion:

  1. A recursion always have a terminating condition (else it will be infinite recursion). In the above case the condition that if (n==1) then stop recursion and return 1.
  2. The function performs some tasks and delegate some. In the above example, the function performs the addition of n with the return value of the Sum(n-1) but delegate the computation of Sum(n-1).

The function (in C language) for the above recursion will be (Not putting a check for n being non-positive)

rec_sum_12.jpg

or to write it in more compact form:

rec_sum_21.jpg

A Iterative version of the above recursion will be

rec_sum_31.jpg

As seen above a recursion can normally be mapped to a loop. So we have two versions a recursive version and an iterative version. Which one of the above do you think is better from a performance point of view?

Lets see what happens when we call the recursive function as sum(3);

Sum(3); will call Sum(2); which will in-turn call sum(1); So the memory stack will have activation records of three functions with each having a local variable n, something like below:

rec_AR.JPG

Also consider the fact that when a function calls another, then the state of the calling function (the value of temporary variables, the Instruction Pointer and other register values) is saved in the memory. This state will be reloaded into the memory when the called function will return the control to the calling function (This is conceptually similar to the Context switch which happens in a muti-processing Operating system when one process is preempted to execute the another process and at sometime the control returns back to this process).

In the iterative version there will be only one function call to Sum(3) and two local variables i & s on the Activation Record(AR) of the function.

We have compared the two for n=3. You may want to compare them for n=1000. The recursive version will take

  • Time: O(n)
  • Memory: O(n)

While the Iterative version will take:

  • Time: O(n)
  • Memory: O(1)

The Time may look the same O(n) for both cases, but the constant multiplier with the recursive version will be much bigger than the Iterative version.

So recursion is a huge overhead. Both from memory point-of-view and execution time point-of-view.

Why should we use Recursion then?

Recursion wins in its Simplicity and Intuitive approach. Not every recursive function can be written in an iterative version with such an ease. For example, Tree traversal functions are very easy & intuitive to write in a recursive manner, but a pain to write the non-recursive version (By non-recursive I does not mean the Stack simulation version of recursive).

If the size of Input is large or Unbounded then recursion is not a good idea.

Head & Tail Recursion:

A Recursive function typically perform some task and call the recursive function.

If a recursive call is made before the function performs its own task, then it is called Head-recursion. (Recursion is performed at the head of the function body).

In the Above Example, In funciton Sum(3) a call to recursive function Sum(2) will be made first and then the return value from Sum(2) will be added with n. This makes the function Sum, a head-recursive function.

Tip: In C language the order of e valuation of operands for operator + is not defined. It means that in the below statement (where x is an int variable and fun1 and fun2 are functions returning int):
x = fun1() + fun2();
whether fun1 will be called first or fun2 is not defined in the language. This has to be defined by the compiler.

A call to a recursive function is tail-recursive if it is made at the end of the function (after the function performs its own tasks).

Just to see the difference, consider the recursive function to traverse a link list:

rec_list_traverse_head.JPG


rec_list_traverse_tail.JPG

If the below link list is send as an input to the above two funcitons:

rec_link.JPG

then the outputs will be:

Head Recursion: 4  3  2  1 (Print the list in reverse order)

Tail Recursion: 1  2  3  4 (Print the list in forward order)

A tail-recursion is very easy to write in the form of a loop. So there should ideally be no reason to write a tail recursion in the code unless you are writing to demonstrate tail-recursion itself. (At least I don't see any convincing example).

Mixed recursions:

We have taken simple example above, There can examples of  recursions also which is neither a head recursion nor a tail recursion. Plus, there can be multiple recursive calls to the function within a function. For example Consider the recursive code to traverse a tree in in-order:

traversal_in.JPG

The recursive call is made at both, the head and tail. such example are difficult to convert to iterative version, because they do not map to a single loop.

------------------------------------------------------------------------------------------------------------

Interview Questions Archive:

         To see all the questions in the category, click here... 

------------------------------------------------------------------------------------------------------------


Post a Comment
*
DevExpress PowerBuilder Web Development Windows Development Languages Software Engineering Databases
iPhone Architecture Secutiry UML & Modeling Operating Systems Networking Testing
Graphics Design Project Management Hardware Open Source Games Development Business Intelligence Visual Studio LightSwitch 2011
MonoDevelop Visual Studio 2010 ASP.NET HTML, DHTML XML PHP JavaScript
Silverlight Web Services WCF Windows Forms WPF Windows Services Dynamic Link Libraries
ActiveX COM, DCOM, ATL C# VB.NET C++ F# Java
Pascal SQL Server Oracle DB2 MS-Access Windows Servers Windows
Linux Unix SAP LINQ .NET Framework ADO.NET Reporting
Crystal Reports SQL Server Reporting Services Igenda Reports Active Reports Adobe Fireworks Arrays & Collections Hosting
Future Trends Android Windows Phone Smart Devices Business M&A Investment & Funding
Web Browsers Internet Explorer Firefox Safari Common Entrepreneurs Students
Consulting Wiki Gadgets MobileMe iCloud iOS Social Media
Facebook Twitter LinkedIn Google+ Microsoft Kinect XBox
Wii Playstation DirectX i OS OS X CIO, CTO, CEO Windows 8
Web Design Expression Blend 4 Photoshop CS5 Creative Suite 5.5 Expression Web 4 Expression Studio 4 Creative SuiteĀ® 5.5 Design
Creative Suite 5.5 Web Creative Suite 5.5 Production Startups Funding M&A Laptops Smart Phones
Desktops Cameras & Camcorders Netbooks Tablets Virtualization Microsoft Surface WordPress
Software Products Cloud Computing Current Affairs Technology TV TV
Earnings XAML E-Commerce MonoTouch Mono for Android Deals Electronics
Mobile Phone Laptop Tablet Book Computer Press Releases Reviews
Products Books Companies Windows Azure SQL Azure Interviews Mac
Web Browsers Symbian Windows Forms WPF Windows Services HTML 5 Office 365
SharePoint 2010 Exchange Server Adobe Visual Studio 2012 iPad Flex / Flash Games
Windows 9
X
 Login
Please login to submit a new post, reply and edit exiting posts, see user profiles, and access more features. If you are not a registered member, Register here.
User Id / Email:
Password:  
Forgot Password | Forgot UserName