Solve four OA string/array/matrix/graph tasks
MetaMachine Learning Engineer · Online AssessmentLast updated
This problem tests foundational proficiency in four distinct domains: string processing, array simulation, matrix transformation, and graph traversal.
Coding Challenge: Meta Core Competencies
Solve the following four independent tasks. Implement your solutions in Python 3.
Task 1: String Character Balance
Given a string s containing only lowercase English letters, return True if every character that appears in the string appears the same number of times, and False otherwise.
is_balanced(s: str) -> bool
Example: is_balanced("aabbcc") returns True, is_balanced("abb") returns False.
Task 2: Linear Movement Simulation
You are given an array of integers positions representing people on a 1D line and a string directions ("L" or "R") of equal length. Every second, each person moves 1 unit in their given direction. If two people move into the same position at the same time, they collide and stop moving permanently. Return the final positions of all people.
final_positions(positions: List[int], directions: str) -> List[int]
Task 3: Matrix Row-Column Swap
Given an n x n binary matrix grid, perform a "flip" operation: for every row i, if grid[i][0] is 1, invert all values in that row (0 becomes 1, 1 becomes 0). After that, for every column j, if the sum of elements in that column is less than n/2, invert all values in that column. Return the final matrix.
transform_matrix(grid: List[List[int]]) -> List[List[int]]
Task 4: Undirected Path Reconstruction
Given an undirected graph represented by an adjacency list adj and two nodes start and end, return the path as a list of nodes. If multiple paths exist, return the one with the fewest number of edges. If no path exists, return an empty list.
find_shortest_path(adj: Dict[int, List[int]], start: int, end: int) -> List[int]
Constraints
- For all tasks, input sizes
nare between 1 and 500. - Matrix elements are strictly 0 or 1.
- The graph is undirected and may contain cycles.
Hints
Hint 1
For string balance, use a frequency dictionary to store counts and check if all values are identical.
Hint 2
For the simulation, track the state of each person (moving vs. stopped) and use a set to monitor occupied positions at each time step.
Hint 3
For the path reconstruction, perform a Breadth-First Search (BFS) while maintaining a parent pointer map to backtrack from the end node to the start node.
Follow-up questions
- How would you optimize the space complexity of the matrix transformation if the matrix was sparse?
- If the graph were weighted, how would the path reconstruction algorithm change?
- In the movement simulation, what is the maximum time complexity in terms of the line length?
Solution walkthrough is for members
Get the full walkthrough with reference code and complexity analysis, plus Kitty Coach at 300 messages a day. Hints, the code runner and community answers stay free.