KCG interview question

- SQL query - implementing algorithm with O(n) complexity