PracHub
QuestionsPremiumLearningGuidesCheatsheetNEW

Quick Overview

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.

  • hard
  • Point72
  • Coding & Algorithms
  • Data Scientist

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.

Part 1: Left-to-Right Function Composition

You are given a list of helper function names and a list of integers to feed into the first function. Implement left-to-right function composition. Write a function `solution(funcs, args)` that applies the helpers in order: 1. The first helper is always `add`, and it receives all values from `args` as positional arguments. 2. Every later helper receives exactly one input: the previous helper's output. 3. Return the final result. Helper behavior: - `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 Your task is to implement the composition logic, not just the helpers.

Constraints

  • 1 <= len(funcs) <= 15
  • funcs[0] == 'add'
  • Each later function name is one of: 'square', 'splitter', 'my_max', 'my_min'
  • 1 <= len(args) <= 100
  • -10^9 <= args[i] <= 10^9

Examples

Input: (['add', 'splitter'], [2, 3])

Expected Output: [2, 3]

Explanation: add(2, 3) = 5, then splitter(5) = [2, 3].

Input: (['add', 'square', 'splitter'], [2, 4])

Expected Output: [18, 18]

Explanation: add(2, 4) = 6, square(6) = 36, splitter(36) = [18, 18].

Input: (['add', 'splitter', 'my_max', 'square'], [5, 2, 1])

Expected Output: 16

Explanation: add = 8, splitter(8) = [4, 4], my_max([4, 4]) = 4, square(4) = 16.

Input: (['add'], [7])

Expected Output: 7

Explanation: Edge case: only one function. Return add(7).

Input: (['add', 'splitter'], [-3])

Expected Output: [-2, -1]

Explanation: Edge case with floor division: floor(-3 / 2) = -2, so splitter(-3) = [-2, -1].

Hints

  1. Build a mapping from function name to the actual helper implementation.
  2. Only the first function consumes all values from `args`; every later function should take the previous result as a single argument.

Part 2: Public Attribute Serialization Utilities

Design serialization utilities inspired by Python mixins. Write a function `solution(attributes)` that behaves as if an object had two mixins: - `DictMixin` with method `to_dict()` - `JSONMixin` with method `to_json()` The input `attributes` represents the object's instance attributes. Your function should: 1. Build the public dictionary representation using only attribute names that do not start with `_`. 2. Build the JSON string for that same public representation. 3. Return both results as a tuple: `(public_dict, json_string)`. 4. If the public dictionary cannot be serialized to JSON, raise `TypeError('Object is not JSON serializable')`. Assume Python 3.7+ dictionary insertion order should be preserved in the output.

Constraints

  • 0 <= len(attributes) <= 100
  • All keys in `attributes` are strings
  • Attributes whose names start with '_' must be excluded
  • Official tests use insertion-ordered dictionaries (Python 3.7+ behavior)
  • Your code must raise `TypeError('Object is not JSON serializable')` for unsupported JSON values

Examples

Input: ({'name': 'Alice', '_token': 'abc', 'age': 30},)

Expected Output: ({'name': 'Alice', 'age': 30}, '{"name": "Alice", "age": 30}')

Explanation: Only public attributes are kept. `_token` is excluded.

Input: ({'_private': 1, '_cache': [1, 2]},)

Expected Output: ({}, '{}')

Explanation: Edge case: all attributes are private, so both outputs represent an empty object.

Input: ({},)

Expected Output: ({}, '{}')

Explanation: Edge case: no attributes at all.

Input: ({'title': 'Book', 'pages': 200, 'tags': ['python', 'oop'], 'meta': {'edition': 2}, '_debug': True},)

Expected Output: ({'title': 'Book', 'pages': 200, 'tags': ['python', 'oop'], 'meta': {'edition': 2}}, '{"title": "Book", "pages": 200, "tags": ["python", "oop"], "meta": {"edition": 2}}')

Explanation: Nested lists and dictionaries are valid JSON, and `_debug` is excluded.

Input: ({'active': True, 'count': None, '_skip': 'x'},)

Expected Output: ({'active': True, 'count': None}, '{"active": true, "count": null}')

Explanation: JSON converts Python `True` to `true` and `None` to `null`.

Hints

  1. Filter the dictionary first; both representations should be built from exactly the same public data.
  2. Use `json.dumps(...)` and catch `TypeError` so you can raise the required message.
Last updated: May 13, 2026

Loading coding console...

PracHub

Master your tech interviews with 7,500+ 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

  • Implement Portfolio Trading Optimizer - Point72 (hard)
  • Implement Election Report and Banking Pipeline - Point72 (hard)
  • Find the Smallest String After One Decrement - Point72 (medium)
  • Find kth missing integer and redundant operations - Point72 (easy)
  • Classify relationships for multiple circle pairs - Point72 (Medium)