Code RoomZ-array computation
MediumPrep Room Coding #1380

Z-array computation

CodingAlgorithms & data structuresMid–Senior~25 min

Given a lowercase string s, compute its Z-array and return it as a list, where z[0] is defined as 0 and z[i] (i>0) is the length of the longest substring starting at i that matches a prefix of s. For 'aabxaab' return [0,1,0,0,3,1,0]. Length up to a few thousand.

Implement
z_array(s: str) → list[int]
Examples
in["aabxaab"]out[0,1,0,0,3,1,0]
What a strong answer looks like

State your approach and its time/space complexity out loud before you optimize. Handle the edge cases (empty input, duplicates, overflow), and say why you chose this over the brute force. Green tests are the floor, not the grade.

0:00 of about 25 min
InputExpectedGot
["aabxaab"][0,1,0,0,3,1,0]not run yetsample