Visa Inc. interview question

Leetcode question, trying practice on leetcode