Feature #5: Update VLAN ID

Implementing the "Update VLAN ID" feature for our "Network" project.


We have a network topology in which several network switches are interconnected in a rectangular grid fashion. The switches are configured with VLAN IDs that are stored in a 2D array. Periodically, VLAN IDs need to be changed. At a specific switch, we will initiate a VLAN ID change request. This request will be propagated to its neighbors with the same VLAN ID, which will then forward it to their neighbors, and so on, until all switches in the topology are covered. The security is configured so that a switch will accept a VLAN ID change request only if the request is sent from a switch with the same VLAN ID and is above, below, to its right or left, or in the array. A VLAN ID change request is started at a switch in row r and column c, with a new VLAN ID specified. All the switches’ VLAN IDs need to be updated under the given constraints.

We’ll be provided with a 2D matrix of integers representing the VLAN IDs of switches at each index. We’ll also have the index of the starting switch and the new VLAN ID. Our task is to determine the VLAN IDs for all of the switches after the change messages have finished propagating.

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

Level up your interview prep. Join Educative to access 70+ hands-on prep courses.