Implement Factorial and Squares
Company: Morgan Stanley
Role: Data Scientist
Category: Coding & Algorithms
Difficulty: medium
Interview Round: HR Screen
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
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
- Start with 1 and multiply values from 2 through n.
Squares of the First n Integers
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
- Use a loop or list comprehension over range(n).