Amazon Interview Question

How would you write a query to get X