Code Room
CodingMediumcod-g817
Subject Z algorithmLevel Mid–Senior~25 minCommon in Algorithms & data structures interviewsIndustries Software development

Question

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.

Vibe coding: describe the solution in plain language (or narrate it) and the coach grades your approach. Generating runnable code from your description is coming next.

Run or narrate your approach, then ask the coach.