Katkat

Process pod logs with global increments and pop-min

ConfluentSoftware Engineer · Technical Screen

Last updated

This problem tests your ability to manage a collection of values under global update operations while maintaining the ability to efficiently extract the minimum value.

Problem Statement

You are monitoring logs from multiple pods. Each pod has a counter representing the number of processed requests, initially set to 0. You need to implement a data structure that supports the following operations:

  1. increment(k, val): Increase the counter of pod k by val.
  2. global_increment(val): Increase the counters of all existing pods by val.
  3. pop_min(): Remove and return the pod ID with the smallest counter value. If there is a tie, return the one with the smallest ID. If multiple pods have the same minimum, remove one.

Constraints

  • Number of pods N up to 10^5.
  • Number of operations M up to 10^5.
  • val is a non-negative integer.

Interface

class PodManager:
    def __init__(self, pod_ids):
        # Initialize all pods with counter 0
        pass

    def increment(self, pod_id, val):
        pass

    def global_increment(self, val):
        pass

    def pop_min(self):
        pass

Example

# Initialize with pods [1, 2]
manager = PodManager([1, 2])
manager.increment(1, 10)
manager.global_increment(5)
# Pod 1: 15, Pod 2: 5
manager.pop_min() # returns 2
manager.pop_min() # returns 1

Hints

Hint 1

Instead of updating every pod during a global increment, maintain a 'global_offset' variable that tracks the total added to all pods.

Hint 2

When updating a specific pod, adjust its stored value relative to the current global_offset so that its 'true' value is (stored_value + global_offset).

Hint 3

Use a Min-Heap to store pairs of (adjusted_value, pod_id) to retrieve the minimum in logarithmic time.

Follow-up questions

  • How would your implementation change if you needed to support removing a specific pod from the system entirely?
  • What is the time complexity of each operation, and can you optimize `global_increment` to be O(1)?
  • How would you handle the scenario where pods are added or removed dynamically during runtime?