Factorial Without Loops or Recursion
Updated
Problem statement
Return the exact factorial of n without using a loop or recursion in your implementation. For this exercise, n is between 0 and 20, inclusive. The factorial is the product of the integers from 1 through n, and 0! = 1.
Use a fixed lookup table, explicit straight-line arithmetic and conditionals, or another method that obeys the restriction. Do not call a library factorial function or delegate repeated multiplication to a helper that loops or recurses. Do not use a comprehension, range product, fold, reduce, or generated runtime loop as a substitute.
Return an exact signed 64-bit integer. The largest allowed result is 20! = 2432902008176640000. The small input bound makes unrolled code or a fixed table sufficient.
Function
factorialWithoutLoops(n: int) → longExamples
Example 1
n = 5return = 1205! = 1 × 2 × 3 × 4 × 5 = 120.
Example 2
n = 0return = 1The product has no factors when n=0, so the empty-product convention gives 0! = 1.
Constraints
- For this exercise, assume 0 <= n <= 20.
More Morgan Stanley Onsite Interview questions
- Generate Strings of Length at Least ThreeONSITE INTERVIEW · Seen Sep 2026
- Maximum Concurrent ProcessesOA · ONSITE INTERVIEW · Seen Sep 2026
- Kth Largest Array ElementONSITE INTERVIEW · Seen Jul 2026
- Run-Length Encode Consecutive Letter RunsONSITE INTERVIEW · Seen Jul 2026
- Maximum Edge Removals for Two TravelersONSITE INTERVIEW · Seen Oct 2025
- Remove K DigitsONSITE INTERVIEW · Seen Oct 2025
- Count Ideal ArraysONSITE INTERVIEW · Seen Aug 2023
- Distinct Palindrome PartitionsONSITE INTERVIEW · Seen Aug 2023
- LFU Cache OperationsONSITE INTERVIEW · Seen Aug 2023
- All-Pairs Shortest PathsOA · ONSITE INTERVIEW · Seen Feb 2022
Similar questions at other companies
- Plus OneDruva · ONSITE INTERVIEW
- Rotate a Flat Image ClockwiseGoogle · ONSITE INTERVIEWGoogle interview questions
- Matrix MultiplicationeBay · ONSITE INTERVIEWeBay interview questions
- Maximum Product of Three IntegersPelatro · ONSITE INTERVIEW
- Climbing StairsAmazon · ONSITE INTERVIEW
- Maximum 69 NumberHSBC · ONSITE INTERVIEWHSBC interview questions
- Roman to IntegerKotak Mahindra Bank · PHONE SCREEN · ONSITE INTERVIEWKotak Mahindra Bank interview questions
- Exact-Sixty Dice Game ValueJane Street · ONSITE INTERVIEWJane Street interview questions
- Find the Missing Number from 1 to nHyperVerge · ONSITE INTERVIEW
- Alternating Index Square SumAccenture · ONSITE INTERVIEWAccenture interview questions
Interview reports that mention this question
Morgan Stanley interview experiences
- Morgan Stanley Apprenticeship and Tech Interviews: 6 Experiences
- Morgan Stanley New Grad Technology Analyst: 2 Experiences
- Morgan Stanley Intern OOP Loops: 5 Experiences (Part 4 of 5)
- Morgan Stanley Intern Earlier Loops: 5 Experiences (Part 3 of 5)
- Morgan Stanley Intern Tech Rounds: 5 Experiences (Part 2 of 5)