FastPrepBuild an Inverted Document Index

Build an Inverted Document Index

Confluent logoConfluent● MediumFULLTIMEONSITE INTERVIEW
Learn

Problem statement

Build an inverted index for an ordered array of documents. Tokenize each document into maximal ASCII letter-or-digit terms, lowercase every term, and associate it with the zero-based IDs of documents containing it.

Return one string per term in ascending term order using term:id1,id2,.... Document IDs are ascending and appear at most once per term.

Function

buildInvertedIndex(documents: String[]) → String[]

Examples

Example 1

documents = ["Red blue red","blue green"]return = ["blue:0,1","green:1","red:0"]

Each term receives one posting per containing document in document order.

Example 2

documents = ["A a A"]return = ["a:0"]

Each term receives one posting per containing document in document order.

Example 3

documents = []return = []

Each term receives one posting per containing document in document order.

Constraints

  • 0 <= documents.length <= 10000.
  • The total input length is at most 10^6.
  • Documents contain printable ASCII characters.

Source note: Author-written prompt excerpt retained for source fidelity.

More Confluent problems

See Confluent hiring insights
public String[] buildInvertedIndex(String[] documents) {
    // Write your code here.
}
documents["Red blue red","blue green"]
expected["blue:0,1", "green:1", "red:0"]
Checking account…