IBM interview question

The SQL was just simple SELECT and GROUP BY, the Python question involves using stacks