Implement Factorial and Squares
Company: Morgan Stanley
Role: Data Scientist
Category: Coding & Algorithms
Difficulty: medium
Interview Round: HR Screen
Write two Python functions for a live coding interview:
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.
2. `squares(n)`: Given a non-negative integer `n`, return a list of squares of the first `n` integers. Clearly state your convention—either `[0^2, 1^2, ..., (n-1)^2]` or `[1^2, 2^2, ..., n^2]`—and handle edge cases such as `n = 0` or invalid negative input.
Quick Answer: This question evaluates programming proficiency in Python, basic algorithmic thinking around iteration versus recursion, mathematical reasoning for factorials and sequences, and robustness in input validation and edge-case handling within the Coding & Algorithms domain.