Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

Factorial Function

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

Your question is Factorial Function. 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

A calculation within Bank of America Mobile Banking needs to evaluate factorial values for small nonnegative integers. Write a function that returns the factorial of n, written as n! = n × (n - 1) × ... × 1.

Use an iterative approach and do not call Python's built-in factorial function.

Formal Specification

Implement factorial(n), where n is an integer. Return an integer equal to the product of every integer from 1 through n. By definition, 0! equals 1.

Constraints

  • 0 <= n <= 20
  • n is an integer
  • The result fits within a signed 64-bit integer
  • Do not use Python's built-in factorial function

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