Question DSA-REC-0079
HardWhich recursion is tail-recursive?
- A.
def f(n): return f(n − 1) + f(n − 2)
- B.
def f(n): return 1 if n == 0 else n * f(n − 1)
- C.
Sign up free to see the answer and a step-by-step explanation.
Popular companies
Prepare company by company
Hiring pattern, syllabus, past questions and mocks for each company.
All companiesPractice by subject
Recursion and Backtracking question
Which recursion is tail-recursive?
def f(n): return f(n − 1) + f(n − 2)
def f(n): return 1 if n == 0 else n * f(n − 1)
Sign up free to see the answer and a step-by-step explanation.
def f(n, acc): return acc if n == 0 else f(n − 1, acc * n)
def f(n): return f(n // 2) + 1