Implement matrix-indexing and expression resolver
Company: Instacart
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: Medium
Interview Round: Onsite
##### 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".
Quick Answer: 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.
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