Search⌘ K

Feature #2: TTL Expiry

Understand how to implement TTL expiry to control message spread in networks represented by n-ary trees. Learn to use DFS to build adjacency lists and BFS to find nodes within TTL distance from the server. This lesson helps you analyze the problem and write efficient code with clear time and space complexity.

Description

To limit the number of messages flooding onto the network, we will set a TTL(Time-to-live) on each message. The number of hops a message will propagate away from the server will not exceed the value of the message’s TTL. In this problem, the server can be at any node in the tree. We want to determine which nodes will successfully receive a message from the server, given a specific TTL value. The network structure is represented by n-ary trees.

We’ll be provided with an n-ary tree network structure, the server device from where the message needs to propagate, and the TTL value for the message. We need to return the nodes after which the TTL value of the message will expire.

Let’s try to understand this better with an illustration:

We are using small TTL values for demonstration purposes. Actual TTL values are usually high.

Solution

We can solve this problem using a combination of DFS and BFS algorithms. First, we’ll do a DFS on the tree and store every node’s parent and children in an adjacency list. Then, we can perform BFS on ...