Company: Ion Group - HackerRank
Difficulty: medium
You are given n messages. For each message: replyTo[i] indicates the index of the message it replies to. If replyTo[i] = -1 , the message starts a new thread. Each message has an associated timestamp . Each message has a text . Organize the messages into conversation threads according to these rules: A message with replyTo[i] = -1 is the parent of a new thread. All replies belong to the same thread as their parent (a reply to a reply belongs to the thread of the original parent). Within each thread: The parent message must appear first. All replies must follow, sorted in ascending order of timestamp. Across threads: threads must be ordered by the timestamp of their parent message, from oldest to newest. Input Format The first line contains a single integer n . Each of the next n lines describes message i (0-indexed, in order) and contains timestamp[i] , replyTo[i] and messageText[i] , separated by single spaces. Output Format Print one line per thread, in the required thread order. Eac