This question evaluates a candidate's ability to design concurrent, high-throughput in-memory subscription matching systems, covering skills in data structure selection, algorithmic complexity analysis, and concurrency control.
You are asked to design and implement the internal logic of an in-memory trade subscription processor in C++. A simplified interface is provided:
using HANDLE = int;
struct Tread {
std::string symbol; // e.g. "AAPL"
int size; // number of shares
std::string flag; // e.g. trade condition / venue flag
};
class TreadProcessor {
public:
TreadProcessor() {}
// Called whenever a new trade arrives from the market feed.
void onNewTread(const Tread &tread);
// Register interest in trades matching a given template.
// Returns a handle that can later be used to unsubscribe.
HANDLE subscribe(const Tread &filter,
std::function<void(const Tread &)> callback);
// Cancel a previous subscription.
void unSubscribe(HANDLE handle);
private:
// Design and add your internal data structures here.
};
You may assume:
filter
of type
Tread
and a
callback
function.
t
delivered via
onNewTread
matches
a subscription
filter
if:
t.symbol == filter.symbol
(exact match), and
size
and
flag
must also match if they are non-zero / non-empty in the filter (i.e., the filter can act as a simple template with optional fields).
Functional requirements:
subscribe(filter, callback)
filter
and
callback
.
HANDLE
that identifies this subscription.
onNewTread(tread)
tread
and invoke their callbacks.
unSubscribe(handle)
handle
so that its callback is no longer invoked for future trades.
Non-functional / design goals:
10^5
active subscriptions.
onNewTread
and
subscribe
.
onNewTread
, and another thread may be adding/removing subscriptions. Discuss how you would handle thread safety.
Task
Describe and justify a design for the internals of TreadProcessor:
private
to support fast matching?
subscribe
,
onNewTread
, and
unSubscribe
be implemented conceptually using those data structures?
Login required