AMD interview question

Leetcode style question, medium difficulty.