Rakuten Interview Question

Standard leetcode style question with followups. Some math involved