Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

Recursive Factorial Implementation

EasyPython00:00
Practice interviewer
In session
5 left
00:00

Your question is Recursive Factorial Implementation. Start with the requirements on the right.

Run and submit as often as you like. When you're ready, talk me through your approach or go straight to the code.

You need to log in / sign up to run or submit.

Problem

Quantiphi data-processing components may need factorial values for small combinatorial calculations. Write a recursive function that computes the factorial of a nonnegative integer n.

The factorial of n, written as n!, is the product of every positive integer from 1 through n. By definition, 0! = 1.

Formal Specification

Implement factorial(n), where:

  • Input: one integer n representing a nonnegative value.
  • Output: an integer equal to n!.
  • The implementation must use recursion rather than a loop or library factorial function.
  • You may assume all inputs satisfy the stated constraints.

Constraints

  • 0 <= n <= 900
  • n is an integer
  • The input is nonnegative
  • The result fits within Python's arbitrary-precision integer representation for the tested inputs

Function Signature

def factorial(n):
Your solutionPython 3
You need to log in / sign up to run or submit.
Run your code to see test output