Solve island and frequency problems | Meta Interview Question