Google | L4 | India | Onsite | DSA | Round 2

Difficulty: EASYCategory: InterviewExperienceFrequency: 1Asked On 2025-06-06Added By Anonymous
Source: Link

Onsite R2


Problem Statement

Design an algorithm to manage birthday reminders for your family tree. You are given a genealogical tree representing your direct ancestors,rooted at you. Each person in the tree has a name and birthday. Your task is to implement a function alarmNextBirthday() that sets an alarm for the next upcoming birthday in the family tree. After that birthday passes, alarmNextBirthday() should be called again to set an alarm for the subsequent upcoming birthday, and so on.


Given:

1class Person { 2 String name; 3 int birthdayMonth; 4 int birthdayDayOfTheMonth; 5 Person parentA; 6 Person parentB; 7} 8

I asked a lot of clarifying questions with some test cases


Finalised on below approach

•for every call fetch current system time
•get next birthday from current system time and return
•no rollback needed, need birthdays for current year only
•return Pair of day,month for alarm call


I gave simple approach to traverse entire tree and get next immediate birthday from current date


Explained that time taken will be O(n) for each alarm call


Interviewer said it is fine and asked to code the solution


I told may be we can optimise it further but he was keened on writing this approach first


Wrote the entire logic by creating a class and helper methods and comments, comparator was little tricky to write


He asked me to test my comparator


Then gave one scenario where it will fail and then I fixed my method


Discussed time, space complexity including recursive stack space


He said it looks good


Follow up question

Now we wont rely on current system date


For every alarm call, trigger next birthday and subsequent call will come on that day


Provided solution to maintain a min heap


during class initialisation build heap of all nodes with custom comparator(with constraints that tree will be fixed after initialisation)


Modified the above used comparator for min heap


For every alarm call, check if heap not empty return top element from heap and return


Provided time complexity as O(n) for class, O(logn) for every alarm call


Asked if he wants to dry run


He told me to dry run with above example which I had written for question 1


Then he said it looks good and that was all from his end


What do you think of the rating I will be getting in this round?

Comments