Nailbiter interview question

Write a code for the given Example?