Process Restricted Friend Requests - Problem

You are given an integer n indicating the number of people in a network. Each person is labeled from 0 to n - 1.

You are also given a 0-indexed 2D integer array restrictions, where restrictions[i] = [xi, yi] means that person xi and person yi cannot become friends, either directly or indirectly through other people.

Initially, no one is friends with each other. You are given a list of friend requests as a 0-indexed 2D integer array requests, where requests[j] = [uj, vj] is a friend request between person uj and person vj.

A friend request is successful if uj and vj can be friends. Each friend request is processed in the given order, and upon a successful request, uj and vj become direct friends for all future friend requests.

Return a boolean array result, where each result[j] is true if the jth friend request is successful or false if it is not.

Note: If uj and vj are already direct friends, the request is still successful.

Input & Output

Example 1 — Basic Restriction Violation
$ Input: n = 3, restrictions = [[0,1]], requests = [[0,2],[2,1],[0,1]]
Output: [true,false,false]
💡 Note: Request [0,2] is allowed since no restriction. Request [2,1] would connect 0-1 indirectly (through 2), violating restriction. Request [0,1] directly violates restriction.
Example 2 — Already Connected
$ Input: n = 3, restrictions = [[0,1]], requests = [[1,2],[1,2]]
Output: [true,true]
💡 Note: First request [1,2] is allowed. Second identical request is still successful since 1 and 2 are already friends.
Example 3 — Multiple Components
$ Input: n = 5, restrictions = [[0,1],[1,2],[2,3]], requests = [[0,4],[1,2],[3,1],[3,4]]
Output: [true,false,false,false]
💡 Note: [0,4] allowed. [1,2] violates direct restriction. [3,1] violates restriction [1,2] indirectly. [3,4] would connect restricted pairs through component {0,4}.

Constraints

  • 2 ≤ n ≤ 1000
  • 0 ≤ restrictions.length ≤ 1000
  • restrictions[i].length == 2
  • 0 ≤ xi, yi ≤ n - 1
  • xi ≠ yi
  • 1 ≤ requests.length ≤ 1000
  • requests[j].length == 2
  • 0 ≤ uj, vj ≤ n - 1
  • uj ≠ vj

Visualization

Tap to expand
Process Restricted Friend Requests012Person 0Person 1Person 2RESTRICTEDRequest Processing[0,2] ✓[2,1] ✗[0,1] ✗Request [0,2]: AllowedRequest [2,1]: Indirect violationRequest [0,1]: Direct violationResult: [true, false, false]Track connections while respecting restrictions
Understanding the Visualization
1
Input
Network with n=3 people, restriction [0,1], requests [[0,2],[2,1],[0,1]]
2
Process
Check each request against current connections and restrictions
3
Output
Return [true,false,false] based on validation
Key Takeaway
🎯 Key Insight: Use Union-Find to efficiently track connected components and validate restrictions before merging
Asked in
Google 12 Facebook 8 Amazon 5
28.5K Views
Medium Frequency
~35 min Avg. Time
427 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen