Python interview with a FAANG engineer: Max people alive



Book a mock interview or coaching session with a FAANG engineer as early as tomorrow on interviewing.io! Sign up here: https://interviewing.io/signup?utm_source=youtube.com&utm_medium=referral&utm_campaign=video_link

Check out other Python interviews: https://interviewing.io/python-interview-questions

Disclaimer: All interviews are shared with explicit permission from the interviewer and the interviewee, and all interviews are anonymous. interviewing.io has the sole right to distribute this content.

source

4 thoughts on “Python interview with a FAANG engineer: Max people alive”
  1. Awesome content and both the interviewee and interviewer did a great job. I think another approach with no death date is just to assume it's infinity, so the original code wouldn't change much. Also, I don't think it matters if the death is ordered before birth year if they're in the same year since it will be processed anyways soon or later.

    Here is my take:
    def get_year_max_alive(self, persons: List[Dict[str, Union[str, int]]]) -> Union[int, None]:
    """
    Complexity: O(N*logN) time and O(N) space
    """
    if len(persons) == 0:
    return None

    # Convert birth and death to events and sort by year. Birth is +1 and death is -1
    events = []
    for person in persons:
    heapq.heappush(events, (person['birth'], 1))
    heapq.heappush(events, (person.get('death', float('inf')), -1))

    # Tracking
    current_alive = 0
    max_alive = 0
    max_year = None

    # Pop heap and track those values
    while events:
    year, count = heapq.heappop(events)

    # Optimization: if we hit float('inf'), then no need to go further.
    if year == float('inf'):
    break

    current_alive += count
    if max_alive < current_alive:
    max_alive = current_alive
    max_year = year

    return max_year

Leave a Reply

Your email address will not be published.

Captcha loading...