Remove the outdated stocks from the queue

4th of February of 2021

You are given a snapshot of a queue of stocks that have changing prices coming in from a stream. Remove the outdated stocks from the queue.

Example:

Strategy

This should be easy, right? I have no idea about stocks, so maybe I'm making wrong assumptions :(

Content image

Assuming the queue comes in chronological order, we should just remove the duplicates keeping the last appearance of each sym

Solution

This solution traverses the queue backwards, and removes the duplicates (using a map to find them). This function creates a new array instead of mutating the original one, hope that's fine! (purists will say it is :>). Big-O algorithmic complexity is O(n) since we just traverse the array once and use a map to check duplicates.

should not do anything if the snapshot is empty ✌︎
should not do anything if the snapshot does not contain outdated items ✌︎
should remove all the outdated items ✌︎