Coding questions · Moloco
Can you visit all rooms and score parentheses?
The first problem evaluates graph traversal and reachability by modeling rooms and keys as an implicit graph, and it is commonly asked to assess reasoning about connectivity and exploration in graph-theoretic settings; this falls under the algorithms/graph theory domain and emphasizes practical application of traversal techniques.
Problem 1: Visit all locked rooms with keys
There are n rooms labeled 0..n-1. All rooms are locked except room 0.
- When you enter room i, you find a set of keys
rooms[i]. - Each key is an integer
kthat unlocks roomk. - You may collect and carry all keys you find.
Task: Given rooms: List[List[int]], return true if you can eventually visit every room starting from room 0, otherwise return false.
Input/Output
- Input:
rooms, whererooms[i]is the list of keys in roomi - Output: boolean
Constraints (typical):
1 <= n <= 10^40 <= rooms[i].length <= 10^4- Keys are in range
0..n-1(may include duplicates)
Problem 2: Compute the score of a balanced parentheses string
You are given a balanced parentheses string s consisting only of '(' and ')'.
Define the score recursively:
"()"has score1- Concatenation: if
AandBare balanced, thenscore(A + B) = score(A) + score(B) - Nesting: if
Ais balanced, thenscore("(" + A + ")") = 2 * score(A)
Task: Return score(s).
Input/Output
- Input: balanced string
s - Output: integer score
Constraints (typical):
1 <= s.length <= 10^5sis guaranteed balanced
Visit All Rooms with Keys
Return whether all rooms can be visited starting from room 0.
Examples
Example 1
Input: ([[1], [2], [3], []],)
Output: True
Explanation: All reachable.
Example 2
Input: ([[1, 3], [3, 0, 1], [2], [0]],)
Output: False
Explanation: Room 2 locked forever.
Score of Balanced Parentheses
Return the recursive score of a balanced parentheses string.
Examples
Example 1
Input: ('()',)
Output: 1
Explanation: Base pair.
Example 2
Input: ('(())',)
Output: 2
Explanation: Nested pair.