Factorial
Updated
Problem statement
Given a non-negative integer n, return n! (read "n factorial"), the product of all integers from 1 to n: n! = 1 * 2 * ... * n. By definition, 0! = 1.
The answer can be larger than a 32-bit integer can hold, so return it as a 64-bit integer.
Function
factorial(n: int) → longExamples
Example 1
n = 5return = 1205! = 1 * 2 * 3 * 4 * 5 = 120.
Example 2
n = 0return = 1There is nothing to multiply, and 0! = 1 by definition.
Example 3
n = 1return = 11! = 1.
Example 4
n = 13return = 622702080013! = 12! * 13 = 479,001,600 * 13 = 6,227,020,800. This is larger than 2,147,483,647, the largest 32-bit integer, so a 32-bit result would be wrong here.
Constraints
0 <= n <= 18
More Zillow Phone Screen questions
- Valid ParenthesesPHONE SCREEN · ONSITE INTERVIEW · Seen Jul 2025
- Build a Hash TableOA · PHONE SCREEN · ONSITE INTERVIEW · Seen Feb 2023
- Reversible Run-Length EncodingPHONE SCREEN · Seen Nov 2022
- First Unique Character in a StringOA · PHONE SCREEN · ONSITE INTERVIEW · Seen Oct 2022
- Cumulative Fibonacci SumOA · PHONE SCREEN · Seen Aug 2021
- First N Fibonacci NumbersOA · PHONE SCREEN · ONSITE INTERVIEW · Seen Jun 2021
- Lowest Common Ancestor in a Binary TreePHONE SCREEN · Seen Sep 2019
- LRU CachePHONE SCREEN · ONSITE INTERVIEW · Seen Oct 2016
- Blackjack Hand ScorePHONE SCREEN · ONSITE INTERVIEW · Seen Feb 2016
- Find the Four Missing Numbers in a FileOA · PHONE SCREEN · Seen Feb 2014
Similar questions at other companies
- Multiply Without the Multiplication OperatorMicrosoft · ONSITE INTERVIEWMicrosoft interview questions
- N-th Fibonacci NumberBank of America · ONSITE INTERVIEWBank of America interview questions
- Checked Recursive SumHeadlands Technologies · PHONE SCREENHeadlands Technologies interview questions
- Fibonacci with Input ValidationMorgan Stanley · ONSITE INTERVIEWMorgan Stanley interview questions
- FactorialAMD · ONSITE INTERVIEWAMD interview questions
- Odd Numbers Up and BackNutanix · PHONE SCREENNutanix interview questions
- Factorial Without MultiplicationWayve · PHONE SCREEN
- Path Between Two Nodes of a Fibonacci TreeDatabricks · ONSITE INTERVIEWDatabricks interview questions
- Power with Integer ExponentsLinkedIn · ONSITE INTERVIEWLinkedIn interview questions
- Integer to English Words: Up to 20 DigitsPeopleStrong · ONSITE INTERVIEWPeopleStrong interview questions