Shortest Alternate Colored Path Problem

Given a directed graph consisting of 'N' nodes labeled from '0' to 'N-1'. Each edge in the graph is colored either 'red' or 'blue'. The graph may include self-edges and parallel edges. You have two arrays, redEdges and blueEdges, each containing edges in the form [i, j], representing an edge from node 'i' to node 'j' with the respective color.

The task is to compute an array answer of size 'N', where answer[i] denotes the length of the shortest path from node '0' to node 'i' with alternating colored edges. If no such path exists, answer[i] should be -1.

Input:

The first line contains an integer ‘T’ representing the number of test cases. Each test case begins with three space-separated integers 'N', 'rlen', and 'blen', indicating the number of nodes, and the sizes of the arrays redEdges and blueEdges respectively.
The following rlen lines each contain two space-separated integers 'i' and 'j', indicating a 'red' edge.
The following blen lines each contain two space-separated integers 'i' and 'j', indicating a 'blue' edge.

Output:

For each test case, output a single line containing 'N' space-separated integers, representing the answer array with the shortest path lengths from node '0' to each node.

Example:

Input:
N = 4, redEdges = [[0, 1], [2, 3]], blueEdges = [[1, 2], [1, 3]]
Output:
[0, 1, 2, 2]
Explanation:

The shortest paths from node ‘0’ are:
Node 1: 0->1, Length: 1
Node 2: 0->1->2, Length: 2
Node 3: 0->1->3, Length: 2
Hence, the result array is [0, 1, 2, 2].

Constraints:

  • 1 ≤ T ≤ 100
  • 1 ≤ N ≤ 200
  • 0 ≤ rlen + blen ≤ 1000

Note:

The graph can be disconnected, and there may only be one red or one blue edge between any two distinct nodes. Implementation should only return the results as output is handled separately.
AnswerBot
4mo

The task is to compute the shortest path from node '0' to each node in a directed graph with alternating colored edges.

  • Create a graph using the given red and blue edges.

  • Use Breadth First Search (BFS) ...read more

Anonymous
11mo
from collections import deque, defaultdict def shortest_alternating_paths(N, redEdges, blueEdges): # Initialize the graph with adjacency lists red_graph = defaultdict(list) blue_graph = defaultdict(li...read more
Help your peers!
Select
Add answer anonymously...

Flipkart Software Developer interview questions & answers

A Software Developer was asked 6mo agoQ. Describe a problem that can be solved using the two-pointer technique.
A Software Developer was asked 9mo agoQ. Given the root of a binary search tree (BST) and an integer k, return the kth la...read more
A Software Developer was asked 9mo agoQ. Implement Depth First Search (DFS) traversal on a simple graph.

Popular interview questions of Software Developer

A Software Developer was asked 9mo agoQ1. Given the root of a binary search tree (BST) and an integer k, return the kth la...read more
A Software Developer was asked 9mo agoQ2. Implement Depth First Search (DFS) traversal on a simple graph.
A Software Developer was asked Q3. Describe a time you used topological sort to find the order of nodes in a graph.
Flipkart Software Developer Interview Questions
Stay ahead in your career. Get AmbitionBox app
play-icon
play-icon
qr-code
Trusted by over 1.5 Crore job seekers to find their right fit company
80 L+

Reviews

10L+

Interviews

4 Cr+

Salaries

1.5 Cr+

Users

Contribute to help millions

Made with ❤️ in India. Trademarks belong to their respective owners. All rights reserved © 2025 Info Edge (India) Ltd.

Follow Us
  • Youtube
  • Instagram
  • LinkedIn
  • Facebook
  • Twitter
Profile Image
Hello, Guest
AmbitionBox Employee Choice Awards 2025
Winners announced!
awards-icon
Contribute to help millions!
Write a review
Write a review
Share interview
Share interview
Contribute salary
Contribute salary
Add office photos
Add office photos
Add office benefits
Add office benefits