Pinterest interview question

It was a DSA LC problem