Question bank › Union Find
Dsa Union Find Easy

Course Prerequisites

You are tasked with determining if a set of courses can be completed based on given prerequisite pairs. Each pair consists of two integers, where the first integer is a course and the second is its prerequisite. You must determine if it is possible to complete all courses without circular dependencies. Input: The first line contains an integer P (1 ≤ P ≤ 1000) representing the number of prerequisite pairs. The next P lines each contain two integers a and b, meaning course 'a' requires course 'b' to be completed first. Output: Output 'SAFE' if it is possible to complete all courses and 'UNSAFE' if there is a cycle. Example: Input: 3 1 2 2 3 3 1 Output: UNSAFE

Key concepts

union_findcoursesprerequisite

Practise this out loud — free

Start a mock interview on THIS exact question — a voice AI interviewer opens with it, pushes back like a real onsite, then hands you an instant scorecard.

🎙 Practise this question now
Part of InterviewLab's verified interview question bank. We show the prompt and concepts to practise with — never a copy-paste solution.