FastPrepValidate CSV References and Field Dependencies

Validate CSV References and Field Dependencies

Stripe logoStripe● MediumNEW GRADPHONE SCREEN
Learn

Problem statement

Validate a CSV document, its cross-column reference rules, and its field-dependency graph. Return the third-column value of every accepted data row, in its original order.

CSV records

lines[0] is the header. Split each line at every comma, preserving empty cells, then remove surrounding ASCII spaces from every cell. This exercise uses unquoted CSV: commas always separate cells, and quotes have no special meaning.

Return an empty array when the document is empty, the header contains fewer than three cells, a trimmed header name is empty, or trimmed header names are not unique.

A data row is structurally valid when it has exactly as many cells as the header and every trimmed cell is nonempty. Skip other rows, including rows missing the third column.

Cross-column references

Each pair in referenceRules is [sourceColumn, targetColumn], using exact, case-sensitive header names. A structurally valid row passes that rule when its source-column value occurs in the target column of at least one structurally valid row. A row must pass every reference rule.

Build all target-value sets from the same snapshot of structurally valid rows. A row rejected by a reference rule still contributes its values to that snapshot. Do not repeatedly delete rows or recompute the sets.

Field dependencies

Each pair in dependencies is [field, prerequisite] and represents a directed edge from the first header name to the second. These are dependencies between columns, not between data rows. Return an empty array if the complete field graph has any directed cycle, including a self-loop or a cycle in an otherwise unused component.

Return an empty array if any reference or dependency pair names an unknown header. Repeated pairs are allowed and do not change the answer. When the header, pair names, and dependency graph are valid, return each accepted row's trimmed third cell. Preserve input order and duplicate output values.

Function

validateCsv(lines: String[], referenceRules: String[][], dependencies: String[][]) → String[]

Examples

Example 1

lines = ["id,parent,label","A,A,Alpha","B,A,Beta","C,Z,Gamma","D,,Missing","E,B, Epsilon ","broken,row"]referenceRules = [["parent","id"]]dependencies = [["label","parent"],["parent","id"]]return = ["Alpha","Beta","Epsilon"]

The row with parent Z fails membership. The empty-cell row and short row are skipped before reference sets are built. The field graph is acyclic, and the accepted labels retain their order.

Example 2

lines = ["a,b,c","x,y,z"]referenceRules = []dependencies = [["a","b"],["b","a"]]return = []

The field graph contains the cycle a to b to a, so the whole document is rejected.

Example 3

lines = ["left,right,out","a,b,keep","b,z,drop"]referenceRules = [["right","left"]]dependencies = []return = ["keep"]

The target snapshot contains a and b. The first row may use b from the second row even though that second row later fails its own reference check.

Constraints

  • 0 <= lines.length <= 2001; a header has at most 30 cells.
  • 0 <= referenceRules.length <= 30 and 0 <= dependencies.length <= 900.
  • Every rule and dependency pair contains exactly two nonempty column-name strings.
  • CSV lines use printable ASCII characters. The total length of all input strings is at most 200000.
  • Every comma is a separator; quoted fields and embedded newlines are outside this exercise.

More Stripe problems

See Stripe hiring insights
public String[] validateCsv(String[] lines, String[][] referenceRules, String[][] dependencies) {
    // write your code here
}
lines["id,parent,label","A,A,Alpha","B,A,Beta","C,Z,Gamma","D,,Missing","E,B, Epsilon ","broken,row"]
referenceRules[["parent","id"]]
dependencies[["label","parent"],["parent","id"]]
expected["Alpha", "Beta", "Epsilon"]
Checking account…