IMC Trading interview question

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