One hour and ten minutes, four questions in total.
Question 1
Very simple — take a string and reorder it by first character, last character, second character, second-to-last character, and so on.
Input: abcde
Output: aebdc
Question 2
Also simple — a string made up only of W, D, L characters. Sort the whole thing so the output repeats in W, D, L order.
Input: WWWLLDDLD
Output: WDLWDLWDL
Input: WLDDL
Output: WDLDL
Input: WWWWLDDL
Output: WDLWDLWW
Question 3
Similar to a LeetCode problem about cyclically rotating a grid.
Given an n x m integer matrix, the matrix can be divided into layers/borders:
- Layer 0 is the outermost border
- Layer 1 is the next border after removing the outermost layer
- and so on
For each border layer:
- Extract all elements along that border in clockwise order, starting from the top-left corner of that layer
- Sort the extracted elements in ascending order
- Write them back starting from the same top-left corner, again in clockwise order
Return the resulting matrix.
Constraints:
1 <= n, m <= 200
-10^9 <= matrix[i][j] <= 10^9
Process the matrix layer by layer. For each layer, first generate the coordinates that the border passes through in clockwise order. Then:
- read out the elements at those coordinates
- sort them
- write them back in the same coordinate order
Watch out for the degenerate cases:
- the innermost layer is only one row
- the innermost layer is only one column
- the innermost layer is a single element
import sys
def border_coords(top, left, bottom, right):
coords = []
# only one row left
if top == bottom:
for c in range(left, right + 1):
coords.append((top, c))
return coords
# only one column left
if left == right:
for r in range(top, bottom + 1):
coords.append((r, left))
return coords
# top edge: left to right
for c in range(left, right + 1):
coords.append((top, c))
# right edge: top to bottom
for r in range(top + 1, bottom + 1):
coords.append((r, right))
# bottom edge: right to left
for c in range(right - 1, left - 1, -1):
coords.append((bottom, c))
# left edge: bottom to top, careful not to repeat the top-left corner
for r in range(bottom - 1, top, -1):
coords.append((r, left))
return coords
def solve_matrix(matrix):
n = len(matrix)
m = len(matrix[0]) if n else 0
layers = (min(n, m) + 1) // 2
for layer in range(layers):
top, left = layer, layer
bottom, right = n - 1 - layer, m - 1 - layer
if top > bottom or left > right:
break
coords = border_coords(top, left, bottom, right)
values = sorted(matrix[r][c] for r, c in coords)
for (r, c), value in zip(coords, values):
matrix[r][c] = value
return matrix
Question 4 (new question)
The gist of it: define two numbers as forming a pair when one can be rotated into the other, e.g. 123 and 312. Given a list of integers, count how many such pairs can be formed.
I went straight for a brute-force approach, comparing every pair of numbers directly. Out of 20 tests I only passed 14, and the rest timed out. I got there eventually once I had enough time left.
Score for this question: 140/300
Total score: 534/600
Discussion
Loading comments…