You are asked to solve two coding tasks.
Given an array of strings strs, group the strings that are anagrams of each other.
Input: strs: string[]
Output: string[][] where each inner array is one anagram group.
Constraints (typical):
1 <= len(strs) <= 10^5
<= 100
allocate() / free() with no extra spaceYou have a fixed memory pool represented by an array mem of 32 slots:
mem
has length
N = 32
.
mem[i]
is a
64-bit unsigned value
.
Implement the following API:
init(mem)
: initialize the pool so all slots are free.
allocate(x) -> handle
: stores value
x
into one free slot and returns a
handle
(an integer index
0..31
). If no space is available, return
-1
.
free(handle)
: marks the slot at
handle
as free.
Key constraint:
N
(no separate boolean/bitmap arrays, hash sets, etc.). Only
O(1)
extra variables are allowed beyond the given
mem
array.
Clarifications to state/assume in your solution (as an interviewer would):
free()
may be called on an invalid or already-freed handle, and what to do in that case.
Design for good time complexity (ideally O(1) per operation).