IMC Trading Interview Question

First is a hard leetcode Q, the second one is implementing a stack.