PracHub
QuestionsPremiumCoachesLearningGuidesInterview Prep
|Home/Coding & Algorithms/Morgan Stanley

Implement Factorial and Squares

Last updated: Jun 15, 2026

Quick Overview

A Morgan Stanley data scientist HR-screen live-coding warm-up: implement Python `factorial(n)` (iteratively, then explain the recursive version) and `squares(n)` returning the squares of the first n integers. Evaluates clean Python, the iteration-vs-recursion trade-off, time and space complexity analysis, input validation, and edge cases like n = 0 and negative input.

  • medium
  • Morgan Stanley
  • Coding & Algorithms
  • Data Scientist

Implement Factorial and Squares

Company: Morgan Stanley

Role: Data Scientist

Category: Coding & Algorithms

Difficulty: medium

Interview Round: HR Screen

##### Question In a live coding interview, write two Python functions and be ready to discuss their behavior. 1. `factorial(n)`: Given a non-negative integer `n`, return `n!`. Implement it **iteratively**, and be prepared to explain how a **recursive** version would differ. State the time and space complexity of each approach. 2. `squares(n)`: Given a non-negative integer `n`, return a list containing the squares of the first `n` integers, in order. Clearly state your convention—either `[0^2, 1^2, ..., (n-1)^2]` or `[1^2, 2^2, ..., n^2]`. For example, with the 0-based convention, `n = 5` should return `[0, 1, 4, 9, 16]`. For both functions, discuss input validation and edge cases such as `n = 0` and invalid negative input.

Quick Answer: A Morgan Stanley data scientist HR-screen live-coding warm-up: implement Python `factorial(n)` (iteratively, then explain the recursive version) and `squares(n)` returning the squares of the first n integers. Evaluates clean Python, the iteration-vs-recursion trade-off, time and space complexity analysis, input validation, and edge cases like n = 0 and negative input.

Related Interview Questions

  • Compute maximum non-overlapping meetings - Morgan Stanley (medium)
  • How do you escape the circle? - Morgan Stanley (medium)
  • Compute win probability in coin-toss game - Morgan Stanley (easy)
  • Count decodings of a digit string - Morgan Stanley (Medium)
  • Compute minimal cost to merge numbers - Morgan Stanley (Medium)
Morgan Stanley logo
Morgan Stanley
Mar 1, 2026, 12:00 AM
Data Scientist
HR Screen
Coding & Algorithms
4
0
Question

In a live coding interview, write two Python functions and be ready to discuss their behavior.

  1. factorial(n) : Given a non-negative integer n , return n! . Implement it iteratively , and be prepared to explain how a recursive version would differ. State the time and space complexity of each approach.
  2. squares(n) : Given a non-negative integer n , return a list containing the squares of the first n integers, in order. Clearly state your convention—either [0^2, 1^2, ..., (n-1)^2] or [1^2, 2^2, ..., n^2] . For example, with the 0-based convention, n = 5 should return [0, 1, 4, 9, 16] .

For both functions, discuss input validation and edge cases such as n = 0 and invalid negative input.

Solution

Show

Submit Your Answer

Sign in to leave a comment

Loading comments...

Browse More Questions

More Coding & Algorithms•More Morgan Stanley•More Data Scientist•Morgan Stanley Data Scientist•Morgan Stanley Coding & Algorithms•Data Scientist Coding & Algorithms
PracHub

Master your tech interviews with 8,500+ real questions from top companies.

Product

  • Questions
  • Learning Tracks
  • Interview Guides
  • Resources
  • Premium
  • For Universities
  • Student Access

Browse

  • By Company
  • By Role
  • By Category
  • Topic Hubs
  • SQL Questions
  • Compare Platforms
  • Discord Community

Support

  • support@prachub.com
  • (916) 541-4762

Legal

  • Privacy Policy
  • Terms of Service
  • About Us

© 2026 PracHub. All rights reserved.