TikTok Interview Question

Write code to implenment the Sort the flags problem/