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

0% completed

Vote For New Content
While the algo produces the correct result it incorrectly calculates the max_len...

stephen

Nov 30, 2022

While the algo produces the correct result it incorrectly calculates the max_length even though the window is invalid.

When start = 0, end = 4, max_repeat = 2, max_len = 4; you will enter the while loop and increment start. Now start = 1 and this still is not a valid window but you will not enter the while loop since 2 !> 2.

Since the iteration before max = 3-0, this time max = 4-1 which is the same answer since you are just shifting the window but you shouldn't be calculating the max_window when the constraint is not met.

I get the idea of using a generalized solution but this can be confusing for someone who is writing out the code and confused about why we are calculating max on an invalid window.

2

0

Comments
Comments

On this page