Level: Unknown Level
Round: Onsite · Type: Coding · Difficulty: 5/10 · Duration: 60 min · Interviewer: Unfriendly
Topics: Union Find, Tree Traversal
Location: San Francisco, CA
Interview date: 2025-12-28
The question asks me to construct the full path from a top-level folder down to a specified target folder, given a folder system represented by three arrays: idList, children, and nameList. I need to return the path as a single string, with folder names ordered from root to target and separated by commas. If the target folder doesn't exist, I should return an empty string.
The coding question I got was:
You are given a representation of a folder system using three parallel arrays, along with a specific folder ID. The folders together form a **forest** (i.e., multiple independent trees).
The inputs are defined as follows:
* **`idList`**
A list of unique integers, where each integer represents a folder ID.
* **`children`**
A list of lists, where `children[i]` contains the IDs of all direct subfolders of the folder whose ID is `idList[i]`.
* **`nameList`**
A list of strings, where `nameList[i]` is the name associated with folder `idList[i]`.
* **`targetId`**
The ID of the folder whose absolute path needs to be determined.
Folders that do **not** appear in any `children[i]` list are considered **top-level folders** (roots).
Return the full path from a top-level folder down to the folder identified by targetId.
, )If the target folder does not exist in the hierarchy, return an empty string ("").
My approach:
children list.Key insights:
Proposed solution:
`python from typing import List, Optional from collections import deque
class Solution: def findFolderPath(self, idList: List[int], children: List[List[int]], nameList: List[str], targetId: int) -> str: n = len(idList) idToName = {} parentMap = {}
for i in range(n):
nodeId = idList[i]
idToName[nodeId] = nameList[i]
# Build parent map
for i in range(n):
nodeId = idList[i]
for childId in children[i]:
parentMap[childId] = nodeId
# Check if targetId exists
if targetId not in idToName:
return ""
# Find path from targetId to root by backtracking
path = []
currentId = targetId
while currentId in idToName:
path.append(idToName[currentId])
if currentId not in parentMap:
# Reached a root node
break
currentId = parentMap[currentId]
# Reverse to get root-to-target path
path.reverse()
return ",".join(path)
`