Google Cloud interview question

How to code this in Java?