Meta Interview Question

2 algorithm question, the second one is harder.