ByteDance Interview Question

1 leetcode medium, with extended question