You are asked to solve two coding tasks.
1) Group anagrams
Given an array of strings strs, group the strings that are anagrams of each other.
-
Two strings are anagrams if they contain the same characters with the same counts (order can differ).
-
Return the groups in any order.
Input: strs: string[]
Output: string[][] where each inner array is one anagram group.
Constraints (typical):
-
1 <= len(strs) <= 10^5
-
Each string length
<= 100
-
Characters are lowercase English letters (unless otherwise stated).
You have a fixed memory pool represented by an array mem of 32 slots:
-
mem
has length
N = 32
.
-
Each
mem[i]
is a
64-bit unsigned value
.
-
Each allocated slot stores exactly one 64-bit 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:
-
You may not use additional data structures proportional to
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):
-
Whether
free()
may be called on an invalid or already-freed handle, and what to do in that case.
-
Whether all 64 bits are available for user data, or whether you are allowed to reserve some bits for metadata.
Design for good time complexity (ideally O(1) per operation).