PracHub
QuestionsPremiumCoachesLearningGuidesInterview Prep

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.

Iterative Factorial

Implement solution(n) that returns n! for a non-negative integer n. Return None for negative input.

Constraints

  • n is an integer

Examples

Input: (0,)

Expected Output: 1

Input: (1,)

Expected Output: 1

Input: (5,)

Expected Output: 120

Input: (7,)

Expected Output: 5040

Input: (-1,)

Expected Output: None

Hints

  1. Start with 1 and multiply values from 2 through n.

Squares of the First n Integers

Implement solution(n) using the 0-based convention: return [0^2, 1^2, ..., (n-1)^2]. Return None for negative input.

Constraints

  • n is an integer

Examples

Input: (0,)

Expected Output: []

Input: (1,)

Expected Output: [0]

Input: (5,)

Expected Output: [0, 1, 4, 9, 16]

Input: (-2,)

Expected Output: None

Hints

  1. Use a loop or list comprehension over range(n).
Last updated: Jun 27, 2026

Loading coding console...

PracHub

Master your tech interviews with 8,000+ 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.

Related Coding 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)
  • Compute minimal cost to merge numbers - Morgan Stanley (Medium)
  • Count decodings of a digit string - Morgan Stanley (Medium)