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

0% completed

Vote For New Content
Test case without cycle?

Matteo

Jul 23, 2023

The task states "Given the head of a Singly LinkedList that contains a cycle" but one of the test cases fails with my code with a NoneType exception when executing "fast = ".

It seems that or is None but if the list indeed has a cycle in it, then every node would have to have a next node or am I missing something?

I see two possible scenarios:

a) There are test cases where the input list does not contain a cycle.

  • Do others feel that the phrasing of the task is misleading as well?
  • What should the return value be in that case, since there is no beginning of a cycle if there is no cycle?

b) All test cases contain a cycle

  • How can ever be None if there is a cycle?

Note:

  1. I can't add the code directly here because if I save it without using the code block it loses the line breaks and if I do save it with the code block some if the right sides of variable assignments keep vanishing from the code
  2. I can't make the attachment button work. It does not respond for me

Because of this I uploaded a screenshot of the solution to showcase my code: https://ibb.co/X7ptC7z

3

0

Comments
Comments
L
Lee 2 years ago

Nothing about your code jumps out at me as incorrect. I might suggest de-nesting for style, but otherwise seems fine.

With coding interviews, unless extra explicitly stated, I don't rely on assumptions like "I will always receive a linked list with a cycle". IMO it's b...