Microsoft Interview Question

problem solved by using moving window