FastPrepFibonacci with Input Validation
Problems/Fibonacci with Input Validation

Fibonacci with Input Validation

Morgan Stanley logoMorgan Stanley● EasyINTERNONSITE INTERVIEW

Updated Nov 2026

QuestionFunctionExamplesConstraints

Problem statement

Source note: Reported in a Morgan Stanley onsite interview for interns. It covers Fibonacci by recursion and by dynamic programming, the tradeoffs of API choices for negative input, and a later follow-up about calculating faster. We chose how the sequence is indexed, the -1 sentinel value, and the number limits.

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

Compute the Fibonacci number F(n), where F(0)=0, F(1)=1, and F(n)=F(n-1)+F(n-2) for n >= 2.

For this exercise, assume the function handles negative input by returning -1. For nonnegative input return the exact integer, without applying a modulus.

Discuss a recursive dynamic-programming implementation and its time complexity. Follow-up: compute the same value asymptotically faster when additional memory is allowed. Explain the API tradeoffs between returning an error sentinel, throwing an exception, and requiring validation at the caller.

Function

fibonacci(n: int) → long

Examples

Example 1

n = 8return = 21

The sequence begins 0,1,1,2,3,5,8,13,21, so F(8)=21.

Example 2

n = -3return = -1

The selected practice API returns -1 for every permitted negative input.

Constraints

  • For this exercise, assume -100 <= n <= 90.
  • Return a signed 64-bit integer. Negative inputs return -1; valid Fibonacci values are nonnegative.

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

  • Every Result of Grouping an ExpressionC3.ai · ONSITE INTERVIEWC3.ai interview questions
  • Fibonacci NumberMicrosoft · ONSITE INTERVIEWMicrosoft interview questions
  • Climbing StairsAmazon · ONSITE INTERVIEW
  • Climbing StairsAMD · ONSITE INTERVIEWAMD interview questions
  • Climb Stairs with One, Two, or Three StepsAgoda · ONSITE INTERVIEWAgoda interview questions
  • Fibonacci NumberJP Morgan Chase · ONSITE INTERVIEWJP Morgan Chase interview questions
  • Climbing StairsExpedia · PHONE SCREEN · ONSITE INTERVIEWExpedia interview questions
  • The Nth Number in the Fibonacci SequenceCapital One · PHONE SCREEN · ONSITE INTERVIEWCapital One interview questions
  • Dice-Walk Winning ProbabilityGoogle · PHONE SCREEN · ONSITE INTERVIEWGoogle interview questions
  • Knight Probability in ChessboardGoldman Sachs · ONSITE INTERVIEWGoldman Sachs interview questions

Interview reports that mention this question

  • Morgan Stanley Intern Earlier Loops: 5 Experiences (Part 3 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 Oldest Loops: 5 Experiences (Part 5 of 5)
  • Morgan Stanley Intern OOP Loops: 5 Experiences (Part 4 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 fibonacci(int n) {
        // Write your code here.
    }

}
Checking account…