Microsoft Interview Question

Algorithm question in mid to hard level