FastPrepFactorial
Problems/Factorial

Factorial

Zillow logoZillow● EasyFULLTIMENEW GRADPHONE SCREENONSITE INTERVIEW

Updated Nov 2026

QuestionFunctionExamplesConstraints

Problem statement

Source note: Asked at Zillow from 2012 to 2014. Two reports place it in a phone interview, one of them coded in a shared online editor after a take-home task. The third places it in a 4-hour in-person interview. The reports come from a new grad candidate and two software candidates who gave no level. In each round it was one of several questions, next to Fibonacci or a Blackjack task. None of them gives more than the task of writing code that computes a factorial.

The reports do not say whether to use recursion or a loop, how large n can be, what to do about overflow, or how the input and answer are passed. Here the function gets n as an integer and returns n! as a 64-bit integer, 0! = 1, and n is at most 18, so every answer is exact in every supported language, including JavaScript numbers. Recursion and a loop are both accepted; the editorial compares them and covers larger n.

Our version is about 90% the same as the reported question.

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) → long

Examples

Example 1

n = 5return = 120

5! = 1 * 2 * 3 * 4 * 5 = 120.

Example 2

n = 0return = 1

There is nothing to multiply, and 0! = 1 by definition.

Example 3

n = 1return = 1

1! = 1.

Example 4

n = 13return = 6227020800

13! = 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
All Zillow interview questionsSee Zillow hiring insights
Saved
class Solution {
    public long factorial(int n) {
        // Write your code here.
    }
}
Checking account…