Coding questions · Instacart
Implement matrix-indexing and expression resolver
This question evaluates a candidate's ability to parse structured text files, perform matrix indexing with a nonstandard origin convention, reconstruct data across multiple blocks, and resolve symbolic variable expressions and dependencies for arithmetic evaluation in the Coding & Algorithms domain.
Question
Given a text file path whose contents include a single block starting with a line like "[2,4]" followed by rows of equal-length characters (e.g., "S3KDA4" … "RTRYYU"), write code to return the character(s) at each coordinate pair, where [0,0] is the bottom-left character of the block. Follow-up 1 (multiple blocks): each block starts with an integer giving the character’s index in a password, then the "[row,col]" header and the character matrix. Return the reconstructed password across all blocks (e.g., blocks for indices 1 and 0 should output "XK"). Follow-up 2 (multiple passwords): continue reading blocks until an index value repeats; the repeat marks the start of a new password. Return the first complete password only. Expression evaluation: Given a target variable computeExp (e.g., T 3) and a list of assignments such as T1=1, T2=2, T3=T4, T4=T5, T5=T2, compute the numeric value of computeExp by resolving dependencies. Follow-up 1: An assignment may contain exactly one + or – (e.g., T3=T4+T 5). Handle integer arithmetic with one operator. Follow-up 2: If the dependency graph contains a cycle (e.g., T4 depends on T5 and T5 depends on T 4), return "IMPOSSIBLE".
Evaluate T-Variable Dependencies with Cycles
You are given a target variable name (e.g., "T3") and a list of assignment strings of the form "Ti=expr". Each variable name is "T" followed by one or more digits. Each expr is one of: (1) an integer (optionally with a leading minus), (2) a single variable name, or (3) a sum/difference of exactly two terms: term "+" term or term "-" term. A term is either an integer (optionally with a leading minus) or a variable name. Whitespace may appear anywhere. Evaluate the numeric value of the target by resolving dependencies. If the dependency graph contains a cycle, return "IMPOSSIBLE". All referenced variables appear on the left-hand side of some assignment and appear at most once on the left-hand side.
Constraints
- 1 <= len(assignments) <= 10000
- Variable names match regex ^T\d+$
- Each variable appears at most once on the left-hand side
- Right-hand side expression has at most one binary operator (+ or -)
- Terms are either integers (optionally with a leading minus) or variables
- All referenced variables are defined in assignments
- If a cycle exists, return "IMPOSSIBLE"
Examples
Example 1
Input: target = "T3", assignments = ["T1=1", "T2=2", "T3=T4", "T4=T5", "T5=T2"]
Output: 2
Explanation: T3 depends on T4 -> T5 -> T2, so T3 = 2.
Example 2
Input: target = "T3", assignments = ["T1=1", "T2=2", "T3=T4+T1", "T4=T3-1"]
Output: IMPOSSIBLE
Explanation: Cycle: T3 depends on T4 and T4 depends on T3.
Hints
- Model dependencies as a directed graph and evaluate using DFS with memoization.
- Track node states: unvisited, visiting, and visited to detect cycles.
- To parse an expression, strip spaces and split on the first '+' or '-' that is not a leading sign.
Approach
Build a dependency graph implicitly by mapping each variable to its right-hand expression and evaluate with DFS. While visiting a variable, mark it as 'visiting'. If it is encountered again in the current path, a cycle exists and the result is 'IMPOSSIBLE'. Use memoization to cache evaluated variables. Parse each expression by removing spaces, then either evaluate a single term (variable or integer) or split on the first '+' or '-' (ignoring a leading sign) and evaluate both terms recursively.
- Time complexity: O(N + E), where N is the number of variables and E is the total number of variable references across expressions
- Space complexity: O(N) for memoization and recursion stack