This is an old one — sharing a question from my campus recruiting interview back in the day.
Given a set of M codes and N timestamps, the ideal shape for the data is an N x M matrix, where the rows are sorted by time and the columns are sorted by the code's lexicographic order. But not every code has data at every timestamp. You're given a stream of std::vector<std::tuple<int, std::string, int>>, where each vector is one batch of data. Each element in the vector is a (Timestamp, code, value) triple, and within a vector it's guaranteed the elements come out sorted by ascending timestamp first, then by the code's lexicographic order. Implement a stream transformer that takes this data stream as input and returns a stream of std::pair<int, vector<int>>. For each pair the stream returns, the first int is one of the given timestamps, and the vector has length M, representing the row of the reconstructed matrix at that timestamp. For any code with no data at a given timestamp, just fill -1 into the corresponding position of the returned vector.
Follow-up 1: what if, for part of the stream, the codes come out of order?
Follow-up 2: what if a small number of records have their timestamps out of order?
Discussion
Loading comments…