Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Home
Talentd Logo
Talentd

Your trusted platform to ace any job interviews, craft the perfect resumes, and land your dream jobs.

P
Featured on
Product Hunt
â–²455
All services are online

Products

  • Resume Review
  • Company Prep Pack
  • DSA Corner
  • Jobs
  • Internships
  • Fresher Jobs
  • Roadmaps
  • Tax Calculator

Resources

  • Articles
  • DRDO Internships

Support

  • Contact Us

DSA & Interview Prep

  • DSA Questions
  • DSA Sheets
  • Company Questions
  • Topics

Company

  • Companies Hiring
  • About
  • Contact
  • Advertisement

Legal

  • Privacy Policy
  • Terms & Conditions
  • Refund Policy
  • Delivery Policy

Popular Skills

Browse All Skills →

Popular Tags

Browse All Tags →

© 2025 Talentd.in - All rights reserved

Privacy PolicyTerms & Conditions
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
DSA Corner
DashboardQuestionsTopicsCompaniesSheets

Talentd Logo
Talentd

Your trusted platform to ace any job interviews, craft the perfect resumes, and land your dream jobs.

P
Featured on
Product Hunt
â–²455
All services are online

Products

  • Resume Review
  • Company Prep Pack
  • DSA Corner
  • Jobs
  • Internships
  • Fresher Jobs
  • Roadmaps
  • Tax Calculator

Resources

  • Articles
  • DRDO Internships

Support

  • Contact Us

DSA & Interview Prep

  • DSA Questions
  • DSA Sheets
  • Company Questions
  • Topics

Company

  • Companies Hiring
  • About
  • Contact
  • Advertisement

Legal

  • Privacy Policy
  • Terms & Conditions
  • Refund Policy
  • Delivery Policy

Popular Skills

Browse All Skills →

Popular Tags

Browse All Tags →

© 2025 Talentd.in - All rights reserved

Privacy PolicyTerms & Conditions
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
DSA Corner
DashboardQuestionsTopicsCompaniesSheets
Talentd Logo
Talentd

Your trusted platform to ace any job interviews, craft the perfect resumes, and land your dream jobs.

P
Featured on
Product Hunt
â–²455
All services are online

Products

  • Resume Review
  • Company Prep Pack
  • DSA Corner
  • Jobs
  • Internships
  • Fresher Jobs
  • Roadmaps
  • Tax Calculator

Resources

  • Articles
  • DRDO Internships

Support

  • Contact Us

DSA & Interview Prep

  • DSA Questions
  • DSA Sheets
  • Company Questions
  • Topics

Company

  • Companies Hiring
  • About
  • Contact
  • Advertisement

Legal

  • Privacy Policy
  • Terms & Conditions
  • Refund Policy
  • Delivery Policy

Popular Skills

Browse All Skills →

Popular Tags

Browse All Tags →

© 2025 Talentd.in - All rights reserved

Privacy PolicyTerms & Conditions
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
DSA Corner
DashboardQuestionsTopicsCompaniesSheets
Talentd Logo
Talentd

Your trusted platform to ace any job interviews, craft the perfect resumes, and land your dream jobs.

P
Featured on
Product Hunt
â–²455
All services are online

Products

  • Resume Review
  • Company Prep Pack
  • DSA Corner
  • Jobs
  • Internships
  • Fresher Jobs
  • Roadmaps
  • Tax Calculator

Resources

  • Articles
  • DRDO Internships

Support

  • Contact Us

DSA & Interview Prep

  • DSA Questions
  • DSA Sheets
  • Company Questions
  • Topics

Company

  • Companies Hiring
  • About
  • Contact
  • Advertisement

Legal

  • Privacy Policy
  • Terms & Conditions
  • Refund Policy
  • Delivery Policy

Popular Skills

Browse All Skills →

Popular Tags

Browse All Tags →

© 2025 Talentd.in - All rights reserved

Privacy PolicyTerms & Conditions
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
DSA Corner
DashboardQuestionsTopicsCompaniesSheets
Back to Problems

729. My Calendar I

Medium58.4% Acceptance
ArrayBinary SearchDesign
Asked by:
A
Amazon
ProblemHints (1)Solutions (9)VideosCompanies (3)Notes

Problem Statement

You are implementing a program to use as your calendar. We can add a new event if adding the event will not cause a double booking.

A double booking happens when two events have some non-empty intersection (i.e., some moment is common to both events.).

The event can be represented as a pair of integers startTime and endTime that represents a booking on the half-open interval [startTime, endTime), the range of real numbers x such that startTime <= x < endTime.

Implement the MyCalendar class:

  • MyCalendar() Initializes the calendar object.
  • boolean book(int startTime, int endTime) Returns true if the event can be added to the calendar successfully without causing a double booking. Otherwise, return false and do not add the event to the calendar.

Example 1:

Input
["MyCalendar", "book", "book", "book"]
[[], [10, 20], [15, 25], [20, 30]]
Output
[null, true, false, true]

Explanation
MyCalendar myCalendar = new MyCalendar();
myCalendar.book(10, 20); // return True
myCalendar.book(15, 25); // return False, It can not be booked because time 15 is already booked by another event.
myCalendar.book(20, 30); // return True, The event can be booked, as the first event takes every time less than 20, but not including 20.

Constraints:

  • 0 <= start < end <= 109
  • At most 1000 calls will be made to book.
Talentd Logo
Talentd

Your trusted platform to ace any job interviews, craft the perfect resumes, and land your dream jobs.

P
Featured on
Product Hunt
â–²455
All services are online

Products

  • Resume Review
  • Company Prep Pack
  • DSA Corner
  • Jobs
  • Internships
  • Fresher Jobs
  • Roadmaps
  • Tax Calculator

Resources

  • Articles
  • DRDO Internships

Support

  • Contact Us

DSA & Interview Prep

  • DSA Questions
  • DSA Sheets
  • Company Questions
  • Topics

Company

  • Companies Hiring
  • About
  • Contact
  • Advertisement

Legal

  • Privacy Policy
  • Terms & Conditions
  • Refund Policy
  • Delivery Policy

Popular Skills

Browse All Skills →

Popular Tags

Browse All Tags →

© 2025 Talentd.in - All rights reserved

Privacy PolicyTerms & Conditions
U
Uber
G
Google

Approach

In #729 My Calendar I, you need to design a calendar system that allows booking time intervals as long as they do not overlap with existing events. The key challenge is efficiently checking whether a new interval conflicts with previously booked ones.

A common approach is to store intervals in a sorted structure such as an ordered set or balanced binary search tree. By keeping events sorted by their start time, you can quickly locate the nearest events before and after the proposed booking. If the new interval does not overlap with these neighbors, the booking can be safely inserted.

Another approach uses a sorted array with binary search to find the correct insertion point. After locating the position, only adjacent intervals need to be checked for conflicts. This reduces unnecessary comparisons and keeps the structure ordered.

These strategies focus on efficient interval management, typically achieving O(log n) lookup time with ordered structures while maintaining minimal additional space.

Complexity

ApproachTime ComplexitySpace Complexity
Ordered Set / Balanced BSTO(log n) per bookingO(n)
Sorted Array with Binary SearchO(log n) search + O(n) insertionO(n)

Video Solution Available

NeetCodeIO

View all video solutions

Problem Hints

Use these hints if you're stuck. Try solving on your own first.

1
Hint 1

Store the events as a sorted list of intervals. If none of the events conflict, then the new event can be added.

Ready to see the solutions?View Solutions

Solutions (9)

Approach 1: Brute Force with List

This approach uses a simple list to store existing bookings. For each new booking, we check against all previously booked intervals to ensure there is no overlap. If an overlap is found, the booking is denied; otherwise, it's added to the list. Although straightforward, this approach can be inefficient for many bookings, as each booking incurs a linear scan through the list of existing bookings.

Time Complexity: O(N) for each booking, where N is the number of existing bookings.
Space Complexity: O(N) for storing the bookings.

CC++JavaPythonC#JavaScript
1#include <stdbool.h>
2#include <stdio.h>
3
4#define MAX_EVENTS 1000
5
6int booked[MAX_EVENTS][

Explanation

This C program maintains a two-dimensional array 'booked' which stores the start and end of each booking. The 'book' function iterates through existing bookings and checks for overlap. If no overlap is found, it adds the new booking.

Approach 2: Balanced Tree Using TreeMap

This approach leverages a data structure like a TreeMap (Java) or SortedDict (Python) to efficiently manage and query intervals. These structures allow us to quickly find the nearest existing intervals to check for overlaps, enabling faster insert operations compared to a simple list. By maintaining the intervals in a sorted order, we can reduce the number of overlaps checks necessary for each new booking.

Time Complexity: O(log N) for each booking due to TreeMap operations.
Space Complexity: O(N).

JavaPythonC++
1import java.util.TreeMap;

    

    

Video Solutions

Watch expert explanations and walkthroughs

My Calendar I - Leetcode 729 - Python

NeetCodeIO
12:5714,889 views

Asked By Companies

3 companies
A
Amazon
U
Uber
G
Google

Prepare for Interviews

Practice problems asked by these companies to ace your technical interviews.

Explore More Problems

Notes

Personal Notes

Jot down your thoughts, approach, and key learnings

0 characters

Similar Problems

Median of Two Sorted ArraysHard
Search in Rotated Sorted ArrayMedium
Find First and Last Position of Element in Sorted ArrayMedium
Search Insert PositionEasy
More similar problems

Related Topics

ArrayBinary SearchDesignSegment TreeOrdered Set

Problem Stats

Acceptance Rate58.4%
DifficultyMedium
Companies3

Practice on LeetCode

Solve with full IDE support and test cases

Solve Now

Frequently Asked Questions

Is My Calendar I asked in FAANG interviews?

Yes, interval scheduling and calendar booking problems are common in technical interviews at companies like Google, Amazon, and Meta. They test knowledge of data structures, interval logic, and efficient search techniques.

What data structure is best for My Calendar I?

An ordered map or balanced binary search tree is typically the best choice. It keeps intervals sorted by start time and allows fast lookups of adjacent intervals, making overlap checks efficient.

What is the optimal approach for My Calendar I?

The optimal approach is to store intervals in a balanced ordered structure such as a TreeMap or ordered set. This allows you to quickly find neighboring intervals and check for overlaps in O(log n) time. It ensures efficient booking even as the number of events grows.

Can My Calendar I be solved using binary search?

Yes, you can maintain a sorted list of intervals and use binary search to locate the insertion point for a new booking. After finding the position, you only need to check the immediate neighbors for overlaps.

2
]
;
7
int
count
=
0
;
8
9
bool
book
(
int
start
,
int
end
)
{
10
for
(
int
i
=
0
;
i
<
count
;
i
++
)
{
11
if
(
start
<
booked
[
i
]
[
1
]
&&
end
>
booked
[
i
]
[
0
]
)
{
12
return
false
;
13
}
14
}
15
booked
[
count
]
[
0
]
=
start
;
16
booked
[
count
]
[
1
]
=
end
;
17
count
++
;
18
return
true
;
19
}
20
21
int
main
(
)
{
22
printf
(
"%d\n"
,
book
(
10
,
20
)
)
;
// True
23
printf
(
"%d\n"
,
book
(
15
,
25
)
)
;
// False
24
printf
(
"%d\n"
,
book
(
20
,
30
)
)
;
// True
25
return
0
;
26
}
2
3
class
MyCalendar
{
4
private
TreeMap
<
Integer
,
Integer
>
calendar
;
5
6
public
MyCalendar
(
)
{
7
calendar
=
new
TreeMap
<
>
(
)
;
8
}
9
10
public
boolean
book
(
int
start
,
int
end
)
{
11
Integer
prev
=
calendar
.
floorKey
(
start
)
;
12
Integer
next
=
calendar
.
ceilingKey
(
start
)
;
13
14
if
(
(
prev
==
null
||
calendar
.
get
(
prev
)
<=
start
)
&&
(
next
==
null
||
end
<=
next
)
)
{
15
calendar
.
put
(
start
,
end
)
;
16
return
true
;
17
}
18
return
false
;
19
}
20
21
public
static
void
main
(
String
[
]
args
)
{
22
MyCalendar
myCalendar
=
new
MyCalendar
(
)
;
23
System
.
out
.
println
(
myCalendar
.
book
(
10
,
20
)
)
;
// True
24
System
.
out
.
println
(
myCalendar
.
book
(
15
,
25
)
)
;
// False
25
System
.
out
.
println
(
myCalendar
.
book
(
20
,
30
)
)
;
// True
26
}
27
}

Explanation

In this Java solution, a TreeMap is used to maintain ordered bookings. 'floorKey' and 'ceilingKey' efficiently find adjacent bookings to check for overlaps with the new booking's start and end times.