Google Interview Question

2 LC medium - hard question