326. Design Document Comment Service
Asked in
Design Document Comment Service
An existing document service stores documents that can be reviewed. Add a comment service that allows reviewers to attach comments to these existing documents.
A comment may be added only when the specified document exists. Comments added to a document must be retained in the order in which they were added.

Class Definition

CommentService(List<String> documentIds)
  • Initializes the service with the identifiers of all existing documents.
  • Every value in documentIds is unique.

Method Signatures

Add a Comment

boolean addComment(String documentId, String reviewerId, String commentText)
  • Adds commentText from reviewerId to the document identified by documentId.
  • Returns true when the document exists and the comment is successfully added.
  • Returns false when documentId does not identify an existing document. No comment is stored in this case.
  • Multiple reviewers may comment on the same document.
  • Identical comments are allowed and are stored separately.

Get Document Comments

List<String> getComments(String documentId)
  • Returns all comments added to the specified document in insertion order.
  • Each returned string uses the format "reviewerId,commentText". The first comma separates the reviewer identifier from the comment text.
  • Returns an empty list when the document does not exist or has no comments.

Deterministic Behavior

  • Comments are always returned in the same order in which they were added.
  • Adding a comment to one document does not affect comments on another document.
  • A rejected comment is never included in later results.

Constraints

  • 1 ≤ documentIds.size() ≤ 10,000
  • 1 ≤ documentId.length() ≤ 100
  • 1 ≤ reviewerId.length() ≤ 100
  • 1 ≤ commentText.length() ≤ 2,000
  • Document identifiers and reviewer identifiers and comment text do not contain commas.
  • All identifiers and comment texts contain printable characters.
  • At most 100,000 method calls are made after initialization.
  • No parameter value is null.

Examples

Example 1: Adding and Reading Comments

CommentService(documentIds = List.of("design-12", "report-27"))
addComment(documentId = "design-12", reviewerId = "reviewer-4", commentText = "Please clarify this section")
Output: true
addComment(documentId = "design-12", reviewerId = "reviewer-9", commentText = "The diagram looks correct")
Output: true
getComments(documentId = "design-12")
Output: List.of("reviewer-4,Please clarify this section", "reviewer-9,The diagram looks correct")
The two comments are returned in the order in which they were added.

Example 2: Document Does Not Exist

CommentService(documentIds = List.of("guide-5", "contract-8"))
addComment(documentId = "invoice-3", reviewerId = "reviewer-2", commentText = "Please verify the total")
Output: false
getComments(documentId = "invoice-3")
Output: List.of()
The comment is rejected because "invoice-3" is not an existing document.

Example 3: Existing Document Without Comments

CommentService(documentIds = List.of("proposal-21"))
getComments(documentId = "proposal-21")
Output: List.of()
The document exists, but no reviewer has added a comment to it.


Please use Laptop/Desktop or any other large screen to add/edit code.