Implement composition and mixin utilities
Company: Point72
Role: Data Scientist
Category: Coding & Algorithms
Difficulty: hard
Interview Round: Take-home Project
Solve the following two Python tasks.
## Part A: Implement function composition
Implement a Python function `compose(*funcs)` that:
1. Accepts any number of functions.
2. Returns a new function representing their left-to-right composition.
3. Passes all positional arguments only to the first function.
4. Passes each function's output as the single input to the next function.
5. Returns the final output.
Use the following helper functions:
- `add(*args)`: returns the sum of all arguments
- `square(a)`: returns `a * a`
- `splitter(a)`: returns `[floor(a / 2), a - floor(a / 2)]`
- `my_max(a)`: if `a` is a number, return it; if `a` is a list, return the maximum element
- `my_min(a)`: if `a` is a number, return it; if `a` is a list, return the minimum element
Examples:
- `compose(add, splitter)(2, 3)` should return `[2, 3]`
- `add(2, 3) -> 5`
- `splitter(5) -> [2, 3]`
- `compose(add, square, splitter)(2, 4)` should return `[18, 18]`
- `add(2, 4) -> 6`
- `square(6) -> 36`
- `splitter(36) -> [18, 18]`
Constraints:
- `1 <= len(funcs) <= 15`
- The first function is always `add`
- Every later function is one of: `square`, `splitter`, `my_max`, `my_min`
## Part B: Implement serialization mixins
Implement two Python mixin classes:
### `DictMixin`
Add a method `to_dict(self)` that converts an object into a dictionary.
### `JSONMixin`
Add a method `to_json(self)` that converts an object into a JSON string.
Requirements for both mixins:
1. Only include attributes whose names do **not** start with `_`.
2. `to_dict()` should return a dictionary of those public attributes.
3. `to_json()` should serialize the same public representation to JSON.
4. If the object cannot be serialized to JSON, raise:
- `TypeError("Object is not JSON serializable")`
Quick Answer: This question evaluates functional programming concepts (function composition and variable-argument handling) and object-oriented serialization patterns (mixins, public-attribute filtering, and JSON encoding), testing competence in Python implementation and robustness to edge cases; it is commonly asked to evaluate reasoning about data flow through composed functions and correct modeling of object-to-JSON transformations in the Coding & Algorithms domain. The level of abstraction is primarily practical application with applied conceptual understanding, focusing on implementation-level skills such as argument routing, type-aware behavior, attribute selection, and JSON serialization error signalling.