Pinterest Interview Question

It was a DSA LC problem