Transform string to adjacency list

Last updated: November 5, 2025

Quick Overview

Given a string representation of a graph where each node is represented by a character and edges are denoted by pairs of characters, transform this string into an adjacency list. The output should be a dictionary where each key is a node and its value is a list of adjacent nodes. For example, for the input "a-b,c-d", the output should be {"a": ["b"], "b": ["a"], "c": ["d"], "d": ["c"]}.

Shopify
Coding & Algorithms
Software Engineer
Shopify
November 5, 2025
Software Engineer
Technical Screen
Coding & Algorithms
Hard

63

5

2,193 solved


Given a string representation of a graph where each node is represented by a character and edges are denoted by pairs of characters, transform this string into an adjacency list. The output should be a dictionary where each key is a node and its value is a list of adjacent nodes. For example, for the input "a-b,c-d", the output should be {"a": ["b"], "b": ["a"], "c": ["d"], "d": ["c"]}.

This coding problem is frequently asked during Technical Screen at Shopify. The interviewer is testing your ability to translate a problem into clean, working code while discussing time and space complexity. Shopify expects candidates to write production-quality code, not just solve the puzzle.

What the Interviewer Expects
  • Quickly identify the optimal approach and its theoretical basis
  • Handle complex algorithm design with multiple interacting components
  • Write concise, elegant code under time pressure
  • Prove correctness of your approach and discuss alternative solutions
  • Optimize beyond the obvious: discuss constant factor improvements
  • Address follow-up variations and explain how the solution generalizes
Key Topics to Cover
Data structure selection and trade-offs
Sorting and searching
Binary search and divide and conquer
Common algorithm patterns (sliding window, two pointers, BFS/DFS)
How to Approach This
  1. Clarify input constraints and edge cases before writing code.
  2. Walk through your approach verbally and confirm with the interviewer before coding.
  3. Start with a brute force solution, then optimize. Mention time and space complexity.
  4. Test your solution with examples, including edge cases like empty input or duplicates.
  5. Consider common patterns: sliding window, two pointers, hash map, BFS/DFS, dynamic programming.
Possible Follow-up Questions
  • Can you solve this iteratively instead of recursively (or vice versa)?
  • What is the worst-case input for your solution?
  • Can you optimize the space complexity of your solution?
  • How would you test this solution thoroughly?
Sharpen Your Skills on Codemia

Practice similar problems with our interactive workspace, get AI feedback, and track your progress.

Practice DSA Problems
Sample Answer
Problem Analysis

The problem requires transforming a string representation of a graph into an adjacency list. The edges between nodes are represented as pairs of characters separated by a delimiter (like commas). The ...

Approach
  1. Initialize an empty dictionary to hold the adjacency list.
  2. Split the input string by commas to isolate each edge. For example, for "a-b,c-d", splitting by ',' gives ['a-b', 'c-d'].
    3...

Submit Your Answer
Markdown supported

Related Questions