MeetCode - Programming Platform | MeetCode - Programming Solutions Platform

Programming Interview Program to Calendar Conflicts with Explanation

Programming Interview Hard OOP 25 views
This problem helps you practice core Programming Interview fundamentals in a practical way. It builds intuition around line, calendar, conflict. Let’s break it down step by step so you can implement it confidently.
Back to Questions

Problem Statement

You're given {x}. Create Calendar class add(start,end) returns YES if added else NO (conflict if overlap). Output results for each add.

Input Format

First line n. Next n lines start end.

Output Format

n lines YES/NO.

Constraints

1

Code Solution

This explanation is written for learning purposes and to help beginners understand the concept clearly.
import sys lines=sys.stdin.read().splitlines() if not lines: sys.exit(0) n=int(lines[0].strip()) class Calendar: def __init__(self): self.arr=[] def add(self,s,e): i=0 j=len(self.arr) while i<j: mid=(i+j)//2 if self.arr[mid][0]<s: i=mid+1 else: j=mid if i>0 and self.arr[i-1][1]>s: return False if i<len(self.arr) and e>self.arr[i][0]: return False self.arr.insert(i,(s,e)) return True cal=Calendar() out=[] for i in range(1,1+n): a,b=map(int,(lines[i] if i<len(lines) else '0 0').split()) out.append('YES' if cal.add(a,b) else 'NO') print('\ '.join(out))

Output Example

Input:
4 1 3 3 5 2 4 6 7
Output:
YES YES NO YES

Common Mistakes

- Misreading input/output format.
- Not handling constraints and edge cases.
- Off-by-one errors in loops.
- Forgetting to reset variables between test cases (if any).

Notes & Extra Practice

Solutions (0)

No solutions submitted yet. Be the first!

Prev Next