KatInterview

Intercept Aircraft and Count User Sessions

Greedy & IntervalsAuto-graded · 23 test cases
EasyAuto-gradedGreedy & Intervalsgreedysortingsimulation

Last updated

Tests greedy deadline scheduling for time-critical interception tasks and event grouping with gap-based sessionization.

You must solve two independent problems and return both answers as a list [aircraft_intercepted, session_count].

Problem A: Maximum Aircraft Interceptions

You operate an anti-aircraft defense system. Each aircraft i is characterized by:

  • altitude[i]: its starting height in meters (positive integer)
  • descent[i]: how many meters it drops each second (positive integer)

Time proceeds in discrete one-second steps starting from t = 0. At each second, the following happens in order:

  1. You may intercept exactly one aircraft (your choice), removing it immediately from the simulation, OR you may choose to do nothing
  2. Then all remaining aircraft descend by their respective descent values

An aircraft lands if its altitude becomes <= 0 after the descent step of any second. When any aircraft lands, the entire process terminates immediately—no further interceptions are possible.

Your goal: Compute the maximum number of aircraft you can intercept before any aircraft lands, assuming you make optimal decisions each second.

Key rules:

  • Interception happens before descent each second
  • You intercept at most one aircraft per second
  • The process stops the instant any aircraft's altitude drops to 0 or below after descent
  • At second t, aircraft have already descended t times (at the end of seconds 0 through t-1) before you act

Problem B: Total Session Count

You are given a log of user activity events. Each event is a pair [user_id, timestamp] where:

  • user_id can be any hashable value (integer, string, etc.)
  • timestamp is a non-negative integer representing seconds
  • Events may appear in any order in the input

For each user, define sessions as follows:

  1. Sort that user's events by timestamp
  2. The first event starts a new session
  3. Each subsequent event either continues the current session or starts a new one:
    • If the time gap from the previous event is <= timeout, it continues the same session
    • If the gap is > timeout, it starts a new session

Your goal: Return the total number of sessions across all users.

Key rules:

  • Sessions are per-user (different users never share a session)
  • Events with identical timestamps for the same user belong to the same session (gap = 0)
  • An empty event list produces 0 sessions

Constraints

  • 0 <= len(altitude) <= 10^4
  • len(altitude) == len(descent)
  • 1 <= altitude[i], descent[i] <= 10^6
  • 0 <= len(events) <= 10^4
  • 0 <= timeout <= 10^6
  • User IDs are hashable (integers, strings, etc.)
  • Timestamps are non-negative integers
  • Events may be in any order

Examples

Example 1

Input: intercept_and_count_sessions([4, 2, 5, 3], [2, 1, 1, 3], [[1, 1], [2, 1], [1, 3], [1, 10], [2, 5]], 2)

Output: [2, 4]

Example 2

Input: intercept_and_count_sessions([], [], [], 0)

Output: [0, 0]

Example 3

Input: intercept_and_count_sessions([10, 20], [3, 5], [[7, 4], [7, 4], [8, 1], [7, 7]], 0)

Output: [2, 3]

Hints

Hint 1

For aircraft: each plane will land after a specific number of seconds if not intercepted. Calculate this survival time for each aircraft.

Hint 2

The most urgent aircraft is the one that will land soonest. Always intercept the aircraft with the earliest landing time to maximize total interceptions.

Hint 3

For sessions: process each user independently by sorting their timestamps, then count how many consecutive pairs have a time gap exceeding the timeout.