KatInterview

Build a CSV Data Frame and Assembly of Sequence Fragments

Data Structure DesignModel answer
MediumModel answerData Structure Designdesign-dsstringstrees-graphs

Last updated

Parse RFC 4180 CSV into an in-memory table with query support, and reconstruct chained sequences from directed or undirected overlapping fragments.

This task consists of two distinct components. Implement a solution for both parts.


Part 1: In-Memory CSV Data Frame

Design an in-memory data frame class initialized from a CSV-formatted string.

Requirements:

  1. Parse the raw input string yourself without using third-party libraries or native CSV modules (e.g., Python's csv module).
  2. The initial line specifies column headers; subsequent lines contain row values.
  3. Your parser must handle RFC 4180 rules:
    • Comma-delimited values.
    • Empty values (e.g., a,,b or trailing commas).
    • Quoted values enclosed in double quotes ("...").
    • Escaped double quotes inside quoted fields represented as pair quotes ("").
    • Embedded line breaks (\n or \r\n) within quoted fields.
    • Commas inside quoted fields.
  4. Raise an exception for invalid formatting (e.g., unclosed quotes, characters immediately following a closing quote without a separator, inconsistent column counts, or duplicate header names).
  5. Expose a retrieval method select(columns=None, predicate=None):
    • columns: optional list of column names to return. If None, return all columns.
    • predicate: optional filter function applied to each row dict. Returns rows where predicate(row) is true.
    • Preserves original row insertion order.

Part 2: Reconstruction of Overlapping Sequences

You are given a list of sequence fragments. Each fragment object contains three properties: start, end, and payload. The end string of one segment matches the start string of another segment.

Base Task: Given a list of fragments that form a single non-branching directed path (one start head, one terminal end), assemble and return the concatenated payload strings in proper sequential order.

Example:

[
  {"start": "bbb", "end": "ccc", "payload": "1"},
  {"start": "aaa", "end": "bbb", "payload": "2"},
  {"start": "ccc", "end": "ddd", "payload": "3"}
]

The connected chain is aaa -> bbb -> ccc -> ddd, yielding the payload order ["2", "1", "3"]. Output: "213".

Follow-up Scenarios:

  1. Undirected Fragments: The endpoints for each fragment are given as an unordered pair (u, v) along with payload. Reconstruct the full ordered sequence of payloads.
  2. Multiple Chains & Validation: The input may contain disconnected valid chains, isolated loops, or branching errors. Write logic to detect and separate all distinct linear sequences, and identify malformed topologies (such as branching nodes with degree > 2 or unexpected cycles).

Hints

Hint 1

For CSV parsing, use a state machine tracking whether you are currently inside quotes and whether the previous character closed a quote.

Hint 2

In Part 2 base problem, identify the head of the chain by looking for a start node whose string key never appears as any fragment's end node.

Hint 3

For undirected fragments, think of endpoints as vertices and fragments as edges. Vertices with degree 1 must be sequence endpoints; interior vertices must have degree 2.