Implement a Lazy Filter over an Iterable

Quick Overview

Implement a lazy Python filter for generators and sequences, preserving order and one-pass consumption while explaining iteration timing and error behavior.

Implement a Lazy Filter over an Iterable

Company: Squarepoint

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Technical Screen

Implement a custom filter that accepts a predicate and an iterable, including a generator, and lazily yields only values for which the predicate is true. Do not materialize the entire input. ### Constraints & Assumptions The predicate is supplied as a callable and may raise exceptions. Preserve input order and propagate predicate or source-iteration errors. Each element should be evaluated at most once. ### Clarifying Questions to Ask - Must the function support one-shot generators and potentially infinite input? - Should predicate errors propagate or be handled by a separate policy? ```hint Delay the loop The caller should be able to request one matching item without exhausting every remaining input value. ``` ### What a Strong Answer Covers - Generator semantics and yielding only matching items. - One-pass consumption, laziness, and constant auxiliary state. - Empty input, no matches, side effects, and error propagation. ### Follow-up Questions - When does the body of a generator function begin executing? - Can requesting one output take unbounded time on an infinite input?

Overview: Implement a lazy Python filter for generators and sequences, preserving order and one-pass consumption while explaining iteration timing and error behavior.

Squarepoint logo
Squarepoint
Mar 15, 2026
mediumSoftware EngineerTechnical ScreenCoding & Algorithms
0
0

Implement a custom filter that accepts a predicate and an iterable, including a generator, and lazily yields only values for which the predicate is true. Do not materialize the entire input.

Constraints & Assumptions

The predicate is supplied as a callable and may raise exceptions. Preserve input order and propagate predicate or source-iteration errors. Each element should be evaluated at most once.

Clarifying Questions to Ask Guidance

  • Must the function support one-shot generators and potentially infinite input?
  • Should predicate errors propagate or be handled by a separate policy?

What a Strong Answer Covers Guidance

  • Generator semantics and yielding only matching items.
  • One-pass consumption, laziness, and constant auxiliary state.
  • Empty input, no matches, side effects, and error propagation.

Follow-up Questions Guidance

  • When does the body of a generator function begin executing?
  • Can requesting one output take unbounded time on an infinite input?