Microsoft Interview Question

Require best algorithm both in time and space