PracHub
QuestionsPremiumCoachesLearningGuidesInterview Prep

Quick Overview

This question evaluates understanding of lazy evaluation and deferred computation, implementation of iterator/generator-like abstractions, and the ability to compose functional transformations (map/filter) while managing performance and memory trade-offs.

  • medium
  • Databricks
  • Coding & Algorithms
  • Machine Learning Engineer

Implement a Lazy Array

Company: Databricks

Role: Machine Learning Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Onsite

Implement a lazily evaluated array abstraction. The object should wrap an underlying sequence and support chained transformations such as `map` and `filter` without executing them immediately. Computation should only happen when the result is materialized, for example through iteration or a method like `to_list()`. After implementing it, explain how you would test that the implementation is truly lazy rather than eagerly computing intermediate results.

Quick Answer: This question evaluates understanding of lazy evaluation and deferred computation, implementation of iterator/generator-like abstractions, and the ability to compose functional transformations (map/filter) while managing performance and memory trade-offs.

Implement a lazily evaluated array abstraction over a sequence of integers. The abstraction should support chained transformations such as map and filter without creating intermediate arrays immediately. Computation must only happen when the result is materialized, such as through iteration or a method like `to_list()`. For this problem, write `solution(nums, operations)` that builds a lazy pipeline over `nums`, applies the operations from left to right, and returns the final materialized list. Supported operations: - `('map', 'add', x)`: replace each value `v` with `v + x` - `('map', 'mul', x)`: replace each value `v` with `v * x` - `('map', 'sub', x)`: replace each value `v` with `v - x` - `('filter', 'gt', x)`: keep values greater than `x` - `('filter', 'lt', x)`: keep values less than `x` - `('filter', 'ge', x)`: keep values greater than or equal to `x` - `('filter', 'le', x)`: keep values less than or equal to `x` - `('filter', 'eq', x)`: keep values equal to `x` - `('filter', 'even')`: keep even values - `('filter', 'odd')`: keep odd values - `('take', k)`: keep only the first `k` values that reach this point in the pipeline A correct implementation should store transformations and only execute them when the final result is requested. How to test true laziness: use map/filter functions with side effects, such as incrementing a counter or raising an exception when an element is touched. Before iteration or `to_list()`, the counter should remain 0. Also test a pipeline ending with `take(k)` and verify that only the minimum number of source elements needed to produce `k` results are evaluated.

Constraints

  • 0 <= len(nums) <= 100000
  • 0 <= len(operations) <= 10000
  • -10^9 <= nums[i], x <= 10^9
  • For `('take', k)`, 0 <= k <= 100000
  • All operations are valid and appear in one of the supported forms

Examples

Input: ([1, 2, 3, 4, 5], [('map', 'mul', 2), ('filter', 'gt', 5)])

Expected Output: [6, 8, 10]

Explanation: Multiply each element by 2 to get [2, 4, 6, 8, 10], then keep values greater than 5.

Input: ([-3, -2, -1, 0, 1, 2, 3], [('filter', 'odd'), ('map', 'add', 1), ('filter', 'ge', 0)])

Expected Output: [0, 2, 4]

Explanation: Odd values are [-3, -1, 1, 3], adding 1 gives [-2, 0, 2, 4], and keeping values >= 0 leaves [0, 2, 4].

Input: ([], [('map', 'add', 5), ('filter', 'even')])

Expected Output: []

Explanation: An empty input remains empty regardless of the lazy operations.

Input: ([1, 2, 3, 4, 5, 6, 7], [('filter', 'even'), ('map', 'mul', 10), ('take', 2)])

Expected Output: [20, 40]

Explanation: Even values are [2, 4, 6], multiplying by 10 gives [20, 40, 60], and taking 2 keeps the first two.

Input: ([2, 2, 2, 3], [('filter', 'eq', 2), ('map', 'add', 1)])

Expected Output: [3, 3, 3]

Explanation: Filter keeps all the 2s, then adding 1 transforms them into 3s.

Input: ([42], [])

Expected Output: [42]

Explanation: With no operations, materializing the lazy array returns the original single element.

Hints

  1. Do not apply each operation to the whole list right away. Store the operations, and build the iterator pipeline only when `to_list()` or iteration is requested.
  2. A good laziness test is to use mapping or filtering callables with side effects: increment a counter or raise an exception on access. The counter should stay at 0 until materialization, and with `take(k)` only as many elements as needed to produce `k` outputs should be touched.
Last updated: Apr 19, 2026

Loading coding console...

PracHub

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

  • Choose the Best Travel Mode - Databricks (medium)
  • Implement an Alternating Tic-Tac-Toe Game - Databricks (hard)
  • Implement a Snapshot Set Iterator - Databricks (medium)
  • Find the Best Commute Mode - Databricks (medium)
  • Partition a Target String by Source Substrings - Databricks (medium)