FastPrepProject and Filter Structured Records
Problem · Array

Project and Filter Structured Records

Learn this problem
MediumStripe logoStripeFULLTIMEPHONE SCREEN
See Stripe hiring insights

Problem statement

You are given a shared schema fieldNames and a collection of records. Each record is an array of string values aligned with the schema, so records[i][j] is the value of field fieldNames[j].

A projection request lists selectedFields. Each row of filters contains exactly two strings: a field name and its expected value. A record matches only when all filter conditions equal the corresponding record values using case-sensitive string equality.

First validate every name in selectedFields and every filter field name. If any name is absent from fieldNames, return exactly [["INVALID"]]. Otherwise, return one projected row for every matching record. Preserve record order, and place each row's values in selectedFields order. An empty filter list matches every record, while a valid request with no matching records returns an empty array.

Function

projectAndFilterRecords(fieldNames: String[], records: String[][], selectedFields: String[], filters: String[][]) → String[][]

Examples

Example 1

fieldNames = ["name","email","country","tier"]records = [["Ada","ada@example.com","US","pro"],["Ben","ben@example.com","CA","pro"],["Cy","cy@example.com","US","basic"]]selectedFields = ["name","email"]filters = [["country","US"],["tier","pro"]]return = [["Ada","ada@example.com"]]

Only Ada has both country = US and tier = pro. The returned row follows the requested name, then email, order.

Example 2

fieldNames = ["name","team"]records = [["Ana","risk"],["Bo","core"]]selectedFields = ["team","name"]filters = []return = [["risk","Ana"],["core","Bo"]]

With no filters, both records match. Input row order is preserved, and each row is projected into team, then name, order.

Example 3

fieldNames = ["name","team"]records = [["Ana","risk"]]selectedFields = ["name"]filters = [["country","US"]]return = [["INVALID"]]

The schema has no country field, so the whole request is invalid before any records are filtered.

Constraints

  • 1 <= fieldNames.length <= 100.
  • Every field name is a non-empty printable ASCII string of length at most 32, and fieldNames contains no duplicates.
  • 0 <= records.length <= 100000, and every record has exactly fieldNames.length values.
  • 1 <= selectedFields.length <= 100; its entries are distinct non-empty printable ASCII field names.
  • 0 <= filters.length <= 100, and every filter has exactly two strings: a non-empty printable ASCII field name and an expected value.
  • Each record value and expected value has at most 100 printable ASCII characters.
  • The total number of values across all records is at most 200000.
  • Field-name and value comparisons are case-sensitive.

More Stripe problems

drafts saved locally
public String[][] projectAndFilterRecords(String[] fieldNames, String[][] records, String[] selectedFields, String[][] filters) {
    // Write your code here
}
fieldNames["name","email","country","tier"]
records[["Ada","ada@example.com","US","pro"],["Ben","ben@example.com","CA","pro"],["Cy","cy@example.com","US","basic"]]
selectedFields["name","email"]
filters[["country","US"],["tier","pro"]]
expected[["Ada", "ada@example.com"]]
checking account