Hone logo
Hone
Problems

Quarantine System with Jest Testing

This challenge asks you to design and implement a simple quarantine system for managing potentially infected items. The system should allow items to be added, marked as infected, quarantined, and released from quarantine. The goal is to create a robust and testable system, with a focus on writing comprehensive Jest tests to ensure its correctness.

Problem Description

You need to build a QuarantineSystem class in TypeScript. This class will manage a list of items and their quarantine status. The system should provide the following functionalities:

  • addItem(item: string): void: Adds a new item to the system. The item should be a unique string.
  • infect(item: string): void: Marks an existing item as infected.
  • quarantine(item: string): void: Quarantines an infected item. An item can only be quarantined if it is already infected.
  • release(item: string): void: Releases a quarantined item from quarantine. An item can only be released if it is currently quarantined.
  • isInfected(item: string): boolean: Checks if an item is infected.
  • isInQuarantine(item: string): boolean: Checks if an item is in quarantine.
  • getItems(): string[]: Returns a list of all items in the system.

The system should maintain the following states for each item: clean, infected, quarantined. An item can only transition through these states in the defined order. Attempting to perform an invalid operation (e.g., quarantining a clean item, releasing a non-quarantined item) should be handled gracefully (e.g., by logging an error or throwing an exception - your choice, but document your approach).

Examples

Example 1:

Input:
  - addItem("item1")
  - addItem("item2")
  - infect("item1")
  - quarantine("item1")
  - isInQuarantine("item1")
  - isInfected("item1")
  - release("item1")
  - isInQuarantine("item1")
  - isInfected("item1")

Output:
  - true
  - true
  - false
  - false

Explanation: "item1" is added, infected, quarantined, and then released. The tests verify the state transitions.

Example 2:

Input:
  - addItem("item3")
  - infect("item3")
  - quarantine("item3")
  - release("item3")
  - release("item3") // Attempt to release an already released item

Output:
  - true
  - true
  - false
  - (Error/Log Message indicating invalid operation)

Explanation: Demonstrates the release operation and handling of an invalid release attempt.

Example 3: (Edge Case)

Input:
  - addItem("item4")
  - infect("item5") // item5 doesn't exist
  - quarantine("item5") // item5 doesn't exist
  - release("item5") // item5 doesn't exist

Output:
  - (Error/Log Message indicating invalid operation for each non-existent item)

Explanation: Tests the system's behavior when attempting to operate on items that do not exist.

Constraints

  • Item Names: Item names are strings and must be unique within the system.
  • State Transitions: The system must enforce the valid state transitions: clean -> infected -> quarantined.
  • Error Handling: Invalid operations (e.g., quarantining a clean item) should be handled gracefully. You can choose to throw an error or log a message. Document your choice.
  • Performance: The system should be efficient enough to handle a reasonable number of items (e.g., up to 1000). No specific performance metrics are required, but avoid unnecessarily complex data structures.

Notes

  • Consider using a Map or Set to efficiently store and manage items.
  • Think about how to represent the state of each item (e.g., an enum or a string).
  • Focus on writing comprehensive Jest tests to cover all functionalities and edge cases. Aim for high test coverage.
  • Document your error handling strategy clearly.
  • The getItems() method should return all items added to the system, regardless of their state.
Loading editor...
typescript