Transform string to adjacency list

Last updated: February 20, 2026

Quick Overview

Given a string representation of a graph where each node is connected by commas and edges are represented by colons, 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 connected nodes. For example, the input "A:B,C;B:A,D;C:B" should produce the output {'A': ['B', 'C'], 'B': ['A', 'D'], 'C': ['B']}.

MongoDB
Coding & Algorithms
Software Engineer
MongoDB
February 20, 2026
Software Engineer
Take-home Project
Coding & Algorithms
Medium

5

8

2,520 solved


Given a string representation of a graph where each node is connected by commas and edges are represented by colons, 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 connected nodes. For example, the input "A:B,C;B:A,D;C:B" should produce the output {'A': ['B', 'C'], 'B': ['A', 'D'], 'C': ['B']}.

Coding interviews at MongoDB focus on problem-solving approach as much as the final solution. The interviewer wants to see you break down the problem, consider edge cases, and optimize iteratively. Communication throughout the process is key.

What the Interviewer Expects
  • Recognize the underlying problem pattern (sliding window, two pointers, BFS/DFS, etc.)
  • Discuss multiple approaches and trade-offs before coding
  • Implement an optimal solution with clean, production-quality code
  • Handle all edge cases including boundary conditions and invalid input
  • Optimize both time and space complexity with clear justification
  • Test your solution systematically with well-chosen examples
Key Topics to Cover
Binary search and divide and conquer
Dynamic programming and memoization
Graph algorithms and traversal
Data structure selection and trade-offs
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 in a single pass?
  • Can you optimize the space complexity of your solution?
  • What happens if the input contains duplicates?
  • How would your solution change if the input was sorted?
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 input format uses commas to separate nodes and colons to define connections. The pattern here is a dire...

Approach
  1. Initialize an empty dictionary to hold the adjacency list.
  2. Split the input string by semicolons to isolate each node and its connections. For example, from the input 'A:B,C;B:A,D;C:B',...

Submit Your Answer
Markdown supported

Related Questions