← Morgan Stanley Interview Insights

Morgan Stanley·Data Scientist·Technical Phone Screen·Intermediate

Intermediate
Jul 2026

Summary

Live coding screen for a Data Scientist role at Morgan Stanley. Two functions, both pretty standard, but they pushed on complexity and edge cases more than I expected.

Questions Asked (2)

Q1

Write an iterative factorial function in Python. Be ready to explain how a recursive version would work and compare the time and space complexity of both approaches.

Algorithms & Data StructuresTechnical Trade-offs
Author's notes

The implementation itself took maybe two minutes.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

Start by writing a clean iterative factorial function with input validation, then explain how a recursive version would work by calling itself with n-1 until reaching the base case. Finally, compare the time and space complexity of both approaches, highlighting that both are O(n) time but iterative is O(1) space while recursive is O(n) space due to call stack.

Pro tip: Mention that Python's recursion limit and lack of tail-call optimization make iterative solutions safer for large inputs, and briefly note that factorial grows extremely fast, so in practice you might use math.factorial or logarithms for large n.

1. Clarify requirements and edge cases

Ask if the input is guaranteed to be a non-negative integer and discuss handling of 0! = 1 and negative inputs. This shows attention to detail and robustness.

2. Implement iterative solution

Write a loop that multiplies an accumulator from 1 to n, initializing result to 1. Include a check for negative input raising ValueError.

3. Explain recursive approach

Describe the recursive function with base case n == 0 returning 1, and recursive case n * factorial(n-1). Mention that it's elegant but uses call stack.

4. Compare time and space complexity

State that both are O(n) time, but iterative uses O(1) space while recursive uses O(n) space due to stack frames. Note Python's recursion limit (~1000) and lack of tail-call optimization.

5. Discuss practical considerations

Mention that for large n, factorial overflows quickly, so in real data science work you might use math.factorial, log-gamma functions, or arbitrary precision libraries. Also note that iterative is generally preferred in production for safety.

Key Points to Mention

  • Time complexity: both iterative and recursive are O(n) because they perform n multiplications.
  • Space complexity: iterative is O(1) auxiliary space; recursive is O(n) due to call stack.
  • Base case for recursion: factorial(0) = 1; iterative loop should start from 1 and multiply up to n.
  • Python's recursion limit and lack of tail-call optimization make recursion risky for large n.
  • Input validation: handle negative numbers and non-integers appropriately.
  • Practical alternatives: math.factorial, math.lgamma for log-factorial, or using Stirling's approximation for large n.

AI-generated suggestions, not part of the candidate's original notes. May be inaccurate — verify before relying on them.

Q2

Write a Python function that returns a list of squares for the first n integers. State clearly whether your list starts from 0 or 1, and handle edge cases like n = 0 and negative input.

Algorithms & Data Structures
Author's notes

I went 0-based without thinking and then second-guessed myself mid-explanation.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

Clarify the indexing convention (0-based or 1-based) upfront, then write a concise function that handles edge cases (n=0, negative n) gracefully. Use a list comprehension for readability and discuss time/space complexity.

Pro tip: In finance, data often starts at index 0, but confirm with the interviewer to avoid ambiguity. Also, mention that returning an empty list for negative n is a design choice—some might prefer raising an error.

1. Clarify requirements

Ask whether the list should start from 0 or 1, and how to handle negative input (return empty list or raise ValueError).

2. Design the function

Decide on the range of integers: if starting from 0, use range(n); if from 1, use range(1, n+1).

3. Implement with edge cases

Write the function using a list comprehension, and include a conditional to handle n <= 0 by returning an empty list.

4. Test and analyze

Test with n=0, n=1, n=5, and negative n. Discuss time complexity O(n) and space complexity O(n).

Key Points to Mention

  • Indexing convention: explicitly state whether the list starts from 0 or 1.
  • Edge case handling: n=0 returns empty list; negative n returns empty list or raises error.
  • Use of list comprehension for concise and efficient code.
  • Time and space complexity: O(n) time and O(n) space.
  • Potential alternative: using map and lambda, but list comprehension is more Pythonic.
  • Consideration of input validation: ensure n is an integer.

AI-generated suggestions, not part of the candidate's original notes. May be inaccurate — verify before relying on them.