Approach

  Blog    |     February 15, 2026

To solve this problem, we need to design a system that efficiently manages the reuse of old certificates in a factory setting. The factory produces items, each associated with a certificate. If an item is found defective, its certificate is marked for reuse instead of being destroyed. The goal is to assign certificates to new items by reusing these old certificates whenever possible.

  1. Initialization: We start with a fixed number of certificates (from 1 to N). All certificates are initially in use by the first set of items produced.
  2. Certificate Assignment: When a new item needs a certificate, we first check if there are any available certificates (from previously defective items). If available, we assign one; otherwise, we return None indicating no certificate is available.
  3. Marking Defective Items: When an item is found defective, its certificate is removed from the set of used certificates and added to the queue of available certificates for future reuse.
  4. Data Structures:
    • Queue (FIFO): To manage available certificates, ensuring that certificates marked defective first are reused first.
    • Set: To track currently used certificates for efficient membership checks and modifications.

Solution Code

from collections import deque
class CertificateManager:
    def __init__(self, n):
        self.available = deque()
        self.used = set(range(1, n + 1))
    def assign_certificate(self):
        if self.available:
            cert = self.available.popleft()
            self.used.add(cert)
            return cert
        return None
    def mark_defective(self, cert):
        if cert in self.used:
            self.used.remove(cert)
            self.available.append(cert)

Explanation

  1. Initialization: The CertificateManager is initialized with n certificates. All certificates are initially in the used set, and the available queue is empty.
  2. Assigning Certificates: The assign_certificate method checks the available queue. If there are certificates available, it assigns the oldest one (FIFO order) by moving it from available to used and returns it. If no certificates are available, it returns None.
  3. Marking Defective Items: The mark_defective method checks if the provided certificate is currently in use. If so, it moves the certificate from used to available for future reuse.
  4. Efficiency:
    • Queue Operations: Enqueue (append) and dequeue (popleft) operations are O(1).
    • Set Operations: Membership checks (in) and removals (remove) are O(1) on average.

This approach efficiently manages certificate reuse by leveraging a queue for available certificates and a set for used certificates, ensuring optimal performance for both assignment and defect marking operations.


Request an On-site Audit / Inquiry

SSL Secured Inquiry