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

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

Company: Google
Role: L4
Interview Stage: Onsite (Round 2)
Location: India
Year: unknown

Original Post Content:

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:

class Person {
    String name;
    int birthdayMonth;
    int birthdayDayOfTheMonth;
    Person parentA;
    Person parentB;
}

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 a simple approach to traverse the entire tree and get the next immediate birthday from the 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 maybe we can optimise it further but he was keen on writing this approach first.

Wrote the entire logic by creating a class and helper methods and comments; comparator was a 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 won't 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(log n) for every alarm call.

Asked if he wants to dry run.
He told me to dry run with the 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