← 返回 snowflake 的题目列表Filter System with Dynamic Blacklist
类型:qbank
Design a filter with processFilter / processInput / emit: emit a value when it appears in the input stream and is not currently blacklisted. Follow-up adds an updateFlag so filter edits re-emit state changes — emit(false, v) when a previously-seen value becomes blocked, emit(true, v) when it becomes visible again.
Problem Overview
Design a filter system that processes a stream of integer inputs against a dynamically maintained blacklist. Implement three methods:
processFilter(int value) — add or remove value from the blacklist filter (dynamic toggle).
processInput(int value) — feed one value from the input stream.
emit(int value) — system output hook for a value.
Rule: when a value arrives via processInput and is not currently in the filter, the system calls emit(value).
You are expected to implement the full class and write your own test cases.
Follow-up
Add an updateFlag and change the signatures:
processFilter(boolean updateFlag, int value)
processInput(int value)
emit(boolean updateFlag, int value)
updateFlag controls whether the filter operation adds or removes value. When a filter update flips the visibility of a value that has already appeared in the input, emit the state change:
emit(false, value) when an already-seen value becomes newly blocked.
emit(true, value) when a blocked-but-already-seen value becomes visible again.
Sample Cases
The published trace (operations in time order):
processFilter +1 +2 -1 +3
processInput 1 3
emit +3 +1 -3
Notes
The base method needs only a set for the current blacklist; the follow-up additionally requires tracking every value seen so far so that a filter edit can replay its effect on already-seen values.
The published trace is terse — clarify the exact ordering and which events trigger emit with the interviewer before coding.
Edge cases to enumerate yourself: a value added then removed from the filter, a value appearing in the input before vs. after it is filtered, and repeated inputs of the same value.