Interview Questions/Coding/Restricted Team Merge Stream

Restricted Team Merge Stream

Preview mode. Log in to edit, run, submit, and save progress.

Hard

Restricted Team Merge Stream

An internal platform processes team merge requests in order. Some pairs of employees are restricted and must never become members of the same merged group. For each merge request, decide whether joining the two requested groups would violate any restriction. If it would not violate a restriction, accept the request and permanently merge the groups. If it would violate a restriction, reject the request and leave all groups unchanged. Input Format: You are given n, an array restrictions, and an array requests. Employee ids are from 0 to n - 1. Output Format: Return an array containing 1 for each accepted request and 0 for each rejected request.

Examples

Example 1
Input:
n = 3
restrictions = [[0, 1]]
requests = [[0, 2], [2, 1]]
Output:
1 0

Explanation: [0, 2] accepted. [2, 1] rejected because it would group restricted pair [0, 1]. Decisions are [1, 0], which is printed as 1 0.

Approach hint

Look for the state that actually matters after each operation.

Common mistake

Skipping assumptions, edge cases, or trade-offs can make an otherwise good answer feel incomplete.

solution.cpp