Hone logo
Hone
Problems

Concurrent Marking System

You are tasked with building a concurrent marking system for a set of documents. In this system, multiple reviewers will mark documents simultaneously. The goal is to efficiently process a large number of documents, ensuring that each document is marked only once, and that the marking process is as fast as possible by leveraging concurrency.

Problem Description

You need to implement a Go program that simulates a concurrent marking system. The system will receive a list of document IDs to be marked. Multiple goroutines (simulating reviewers) will pick up these document IDs and mark them.

Key Requirements:

  1. Uniqueness: Each document ID must be marked exactly once. No document should be marked by multiple goroutines.
  2. Concurrency: The marking process should be concurrent. Multiple goroutines should be able to pick up and process document IDs simultaneously.
  3. Efficiency: The system should be designed to handle a large volume of documents and reviewers efficiently.
  4. Completion Notification: The program should signal when all documents have been marked.

Expected Behavior:

  • The program will take a slice of integers representing document IDs.
  • A pool of goroutines will be launched to pick up and "mark" these documents.
  • "Marking" can be simulated by a time-consuming operation (e.g., time.Sleep).
  • A mechanism must be in place to ensure that a document ID is only processed by one goroutine.
  • The main program should wait until all document IDs from the input slice have been successfully marked before exiting.

Edge Cases:

  • Empty input slice of document IDs.
  • A very large number of document IDs.
  • A large number of concurrent reviewers.

Examples

Example 1:

Input: documentIDs = [101, 102, 103, 104, 105] Number of Reviewers: 3

Output: (A sequence of print statements indicating which reviewer marked which document, and a final message when all are marked. The order of marking will vary due to concurrency.)

Reviewer 1 marked document 101
Reviewer 2 marked document 103
Reviewer 3 marked document 102
Reviewer 1 marked document 104
Reviewer 2 marked document 105
All documents have been marked.

Explanation: Three reviewers concurrently pick up document IDs. Each ID is picked up and marked by only one reviewer. The system continues until all IDs are processed.

Example 2:

Input: documentIDs = [] Number of Reviewers: 5

Output:

All documents have been marked.

Explanation: If there are no documents to mark, the system should immediately indicate completion.

Example 3:

Input: documentIDs = [201, 202, 203, 204, 205, 206, 207, 208, 209, 210] Number of Reviewers: 10

Output: (Similar to Example 1, but with potentially more interleaved output and faster completion.)

Explanation: Demonstrates that with more reviewers than documents, documents are likely picked up very quickly, and the system efficiently completes.

Constraints

  • The number of document IDs will be between 0 and 100,000.
  • The number of concurrent reviewers (goroutines) will be between 1 and 1,000.
  • Document IDs are positive integers.
  • The simulated marking operation (time.Sleep) should be short (e.g., 50ms) to keep execution time reasonable for testing.

Notes

  • Consider using a channel to distribute document IDs to the worker goroutines.
  • A mechanism is needed to track which documents have already been picked up to ensure uniqueness. A sync.Mutex protecting a map or a sync.Map are common patterns for this.
  • The sync.WaitGroup is a useful tool for waiting for all goroutines to complete their work.
  • Think about how to gracefully shut down the worker goroutines once all tasks are done.
Loading editor...
go