Factorial of a Number
Updated
Problem statement
Given a non-negative integer n, return n! (read "n factorial"), the product of all whole numbers from 1 to n. By definition, 0! = 1.
The answer grows quickly and does not fit in a 32-bit integer once n reaches 13, so return it as a 64-bit integer.
Function
factorial(n: int) → longExamples
Example 1
n = 6return = 7206! = 1 * 2 * 3 * 4 * 5 * 6 = 720.
Example 2
n = 0return = 10! is 1 by definition: it is the product of no numbers.
Example 3
n = 13return = 622702080012! = 479,001,600, so 13! = 13 * 479,001,600 = 6,227,020,800. This is larger than the biggest 32-bit integer, 2,147,483,647.
Constraints
0 <= n <= 20
More Bank of America Onsite Interview questions
- Centered Star PyramidONSITE INTERVIEW · Seen Nov 2025
- Employees Without a ManagerONSITE INTERVIEW · Seen Nov 2025
- Longest Substring Without Repeating CharactersONSITE INTERVIEW · Seen Nov 2025
- N-th Fibonacci NumberONSITE INTERVIEW · Seen Nov 2025
- Reverse Each Word in a SentenceONSITE INTERVIEW · Seen Nov 2025
- Second Highest SalaryONSITE INTERVIEW · Seen Nov 2025
- Three Numbers That Add Up to a TargetONSITE INTERVIEW · Seen Apr 2025
- Duplicate RowsONSITE INTERVIEW · Seen Oct 2023
- Palindrome String CheckONSITE INTERVIEW · Seen Sep 2023
- Second Largest Element in an ArrayONSITE INTERVIEW · Seen Jun 2023
Similar questions at other companies
- Multiply Without the Multiplication OperatorMicrosoft · ONSITE INTERVIEWMicrosoft interview questions
- Fibonacci with Input ValidationMorgan Stanley · ONSITE INTERVIEWMorgan Stanley interview questions
- FactorialAMD · ONSITE INTERVIEWAMD interview questions
- 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
- Every Result of Grouping an ExpressionC3.ai · ONSITE INTERVIEWC3.ai interview questions
- Pow(x, n)Bloomberg LP · PHONE SCREEN · ONSITE INTERVIEWBloomberg LP interview questions
- Integer Power Without a Library CallExpedia · ONSITE INTERVIEWExpedia interview questions
- Josephus SurvivorApple · ONSITE INTERVIEWApple interview questions