← Grammarly Interview Insights

Grammarly·Software Engineer·Technical Phone Screen·Intermediate

IntermediatePrefer not to say
Jun 2026

Summary

Grammarly software engineer interview, got a linked list problem that I thought I knew cold but the follow-up tripped me up a bit.

Questions Asked (1)

Q1

Given the head of a singly linked list, detect whether it contains a cycle in O(n) time and O(1) space. Follow-up: if a cycle exists, find the exact node where it begins.

Algorithms & Data Structures
Author's notes

The basic cycle detection part went fine, slow and fast pointer, they meet inside the cycle and you return true.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

Use Floyd's cycle-finding algorithm (tortoise and hare) to detect a cycle in O(n) time and O(1) space. For the follow-up, after detection, reset one pointer to the head and move both pointers one step at a time until they meet; that meeting point is the start of the cycle. Explain the algorithm clearly, justify the time and space complexity, and handle edge cases.

Pro tip: Mention that you can also use Brent's algorithm for slightly better constant factors, but Floyd's is more commonly expected. Also, be prepared to prove why the two-pointer approach works, as interviewers often ask for the mathematical reasoning.

1. Clarify and confirm requirements

Restate the problem to ensure you understand: detect a cycle in a singly linked list with O(n) time and O(1) space, and if a cycle exists, return the node where it begins. Ask if the list can be modified (it shouldn't be) and if there are constraints on the number of nodes.

2. Explain the two-pointer approach

Describe using two pointers, slow and fast, both starting at the head. Slow moves one step at a time, fast moves two steps. If they meet, a cycle exists; if fast reaches null, no cycle.

3. Find the cycle start

After detecting a cycle, reset one pointer to the head and keep the other at the meeting point. Move both one step at a time; the node where they meet again is the start of the cycle.

4. Analyze complexity and edge cases

State that time complexity is O(n) because each pointer traverses at most the length of the list plus cycle length, and space is O(1) since only two pointers are used. Discuss edge cases: empty list, single node, cycle at head, no cycle.

5. Code and test

Write clean code for the detection and start-finding functions. Walk through a small example to verify correctness, and consider mentioning alternative algorithms like hashing (O(n) space) to contrast.

Key Points to Mention

  • Floyd's cycle-finding algorithm (tortoise and hare) and its O(n) time, O(1) space complexity.
  • Proof of why the two-pointer approach works: if there's a cycle, the fast pointer will eventually meet the slow pointer.
  • Mathematical reasoning for finding the cycle start: the distance from head to cycle start equals the distance from meeting point to cycle start (modulo cycle length).
  • Edge cases: empty list, single node, cycle at head, no cycle, and how the algorithm handles them.
  • Alternative approaches like using a hash set (O(n) space) and why they are less optimal.
  • Brent's algorithm as an alternative with similar complexity but fewer comparisons.

AI-generated suggestions, not part of the candidate's original notes. May be inaccurate — verify before relying on them.