Grokking the Coding Interview: Patterns for Coding Questions
Ask Author
Back to course home

0% completed

Vote For New Content
Josh Osborne
Possible Incorrect Implementation

Josh Osborne

Feb 16, 2024

I keep trying to work through why the solution code will ensure that if we have a result string of length equal to the input string it assumes we correctly interlaced the characters. If you feed in "AAAA" the result string will come out as "AAAA" and have the same length as the input string when we want it to output and empty string.

We can simply check if the char == prevChar and return the empty string if thats the case.

What am I not understanding?

Here's how I would amend the while loop:

while maxHeap:       freq,char = heappop(maxHeap)       if char == prevChar:         return ""       resultString.append(char)       if prevChar and -prevFreq > 0:         heappush(maxHeap,[prevFreq,prevChar])       prevChar = char       prevFreq = freq + 1

0

0

Comments
Comments