FastPrepBuild a Log Inverted Index
Problems/Build a Log Inverted Index

Build a Log Inverted Index

Datadog logoDatadog● MediumFULLTIMEONSITE INTERVIEW
QuestionFunctionExamplesConstraints

Problem statement

Source note: The original author reports two coding rounds that provide logs and ask to build an index or reverse index, without giving the schema or output. This practice version constructs complete term-to-record posting lists and defines tokenization, case, duplicate handling, serialization, ordering and bounds. The task matches the report at about 65%.

Build an inverted index for the records in logs. A term is a nonempty token separated from other tokens by exactly one ASCII space. An empty log has no terms. Compare complete terms case-sensitively.

For every distinct term, collect the zero-based indices of logs containing it. Include a log index only once for that term, even when the term appears repeatedly in that log. Equal log strings at different indices remain separate records.

Return one string per term in increasing lexicographic term order. Each string is term:i,j,..., with its record indices in increasing numeric order, ordinary decimal indices and no spaces. Return an empty array if no term occurs. For example, an entry cpu:0,12 means that logs 0 and 12 contain the complete term cpu.

Function

buildLogIndex(logs: String[]) → String[]

Examples

Example 1

logs = ["cpu cpu db","db cache","cpu"]return = ["cache:1","cpu:0,2","db:0,1"]

Repeated cpu in record 0 contributes its index only once.

Example 2

logs = ["","a","a"]return = ["a:1,2"]

The empty record retains index 0, so the equal later records keep their original indices 1 and 2.

Example 3

logs = ["Dog dog","datadog"]return = ["Dog:0","datadog:1","dog:0"]

Terms match whole tokens and preserve case. ASCII lexicographic order places uppercase D before lowercase d.

Constraints

  • 0 <= logs.length <= 200
  • 0 <= logs[i].length <= 2000
  • The sum of all log lengths is at most 20000.
  • Each term has between 1 and 30 characters, using only ASCII letters, digits, underscores and hyphens.
  • A nonempty log has exactly one ASCII space between terms, with no leading or trailing space.

More Datadog problems

  • Query Log MatchPHONE SCREEN · ONSITE INTERVIEW · Seen Sep 2026
  • Delete a Filesystem TreePHONE SCREEN · ONSITE INTERVIEW · Seen Jul 2026
  • Grouped-Query Attention Forward PassPHONE SCREEN · Seen Jun 2026
All Datadog interview questionsSee Datadog hiring insights
Saved
public String[] buildLogIndex(String[] logs) {
    // Write your code here.
}
Checking account…