Problem · Design

Stateful Log Transform and Search

Learn this problem
MediumRippling logoRipplingFULLTIMEPHONE SCREEN

Problem statement

Process an ordered operation sequence for a logger that stores transformed logs in insertion order.

  • ["REMOVE", log, target]: delete every non-overlapping literal occurrence of the non-empty target from left to right, store the result, and output its zero-based insertion index.
  • ["TRUNCATE", log, n]: keep the first n characters, store the result, and output its zero-based insertion index.
  • ["CAPITALIZE", log]: convert every ASCII lowercase letter to uppercase, leave all other characters unchanged, store the result, and output its zero-based insertion index.
  • ["SEARCH", deduplicate, keyword1, ...]: use case-sensitive literal-substring OR matching over stored logs. Visit logs in insertion order. If deduplicate is true, output each matching log once. If it is false, output the log once for each distinct input keyword it contains, in keyword-input order. Repeated keyword arguments count once.

The method returns one row per operation. A write row contains its insertion index as one decimal string. A search row contains its ordered matching logs.

Function

processLogs(operations: String[][]) → String[][]

Examples

Example 1

operations = [["REMOVE","banana bandana","ana"],["CAPITALIZE","Mix 42"],["TRUNCATE","abcdef","3"],["SEARCH","true","A","band"],["SEARCH","false","a","b"]]return = [["0"],["1"],["2"],["bna band"],["bna band","bna band","abc","abc"]]

Removing non-overlapping ana occurrences stores bna band. The deduplicated search returns it once. The non-deduplicated search emits it for both a and b, then emits abc for those two keywords.

Example 2

operations = [["CAPITALIZE","alpha"],["TRUNCATE","alphabet","5"],["REMOVE","aaaa","aa"],["SEARCH","true","alpha","ALP"],["SEARCH","false","a","aa"]]return = [["0"],["1"],["2"],["ALPHA","alpha"],["alpha"]]

Search is case-sensitive. The empty stored log matches neither keyword. With deduplication disabled, lowercase alpha contains a but not aa.

Example 3

operations = [["TRUNCATE","hello","0"],["SEARCH","true","x"],["REMOVE","xxxyxx","xx"],["SEARCH","false","x","x","y"]]return = [["0"],[],["1"],["xy","xy"]]

Truncating to zero stores an empty log. Removing xx left to right from xxxyxx stores xy. Repeated keyword x counts once, so xy is emitted for distinct keywords x and y.

Constraints

  • 1 <= operations.length <= 10^4.
  • Logs, non-empty removal targets, and keywords contain at most 200 printable ASCII characters.
  • Each truncation length is between 0 and the corresponding input-log length.
  • Each search supplies at least one keyword. Its deduplicate value is true or false.
  • The sum of stored log count * distinct keyword count across all searches is at most 2 * 10^5.

More Rippling problems

drafts saved locally
public String[][] processLogs(String[][] operations) {
  // write your code here
}
operations[["REMOVE","banana bandana","ana"],["CAPITALIZE","Mix 42"],["TRUNCATE","abcdef","3"],["SEARCH","true","A","band"],["SEARCH","false","a","b"]]
expected[["0", "1", "2", "bna band", "bna band", "bna band", "abc", "abc"]]
checking account