FastPrepFactorial Without Loops or Recursion
Problems/Factorial Without Loops or Recursion

Factorial Without Loops or Recursion

Morgan Stanley logoMorgan Stanley● EasyINTERNONSITE INTERVIEW

Updated Nov 2026

QuestionFunctionExamplesConstraints

Problem statement

Source note: Reported in a Morgan Stanley onsite interview for interns. The campus author asks for factorial code written without any loop or recursion. No limits or preferred technique are given. Here n is from 0 through 20. A fixed lookup or straight-line arithmetic is accepted. Library factorial calls and handing the looping to something else are not allowed. The solutions do not claim to be a general factorial method.

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

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

Examples

Example 1

n = 5return = 120

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

Example 2

n = 0return = 1

The 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 Intern Oldest Loops: 5 Experiences (Part 5 of 5)

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)
All Morgan Stanley interview questionsSee Morgan Stanley hiring insights
Saved
class Solution {
    public long factorialWithoutLoops(int n) {
        // Write your code here.
    }

}
Checking account…