Detect Where a Short Link Redirect Chain Starts Looping

Last updated: November 29, 2026

Quick Overview

Shared TikTok links pass through a chain of redirects, given as a map from each URL to the next. Starting from a URL, return the first URL that is part of a redirect loop, or None if the chain terminates, using O(1) extra memory.

TikTok
Coding & Algorithms
Software Engineer
TikTok
November 29, 2026
Software Engineer
Phone Screen
Coding & Algorithms
Easy

0

0

0 solved


Shared TikTok links pass through a chain of redirects, given as a map from each URL to the next. Starting from a URL, return the first URL that is part of a redirect loop, or None if the chain terminates, using O(1) extra memory.

Link sharing and the share-to-other-apps flow depend on redirect resolution, and misconfigured campaigns can create loops that hang crawlers. This phone screen question checks Floyd's cycle detection on an implicit linked list and the reasoning for why resetting one pointer finds the loop entry.

What the Interviewer Expects
  • Recognizes the redirect map as an implicit linked list
  • Gives the easy visited-set answer first, then the O(1) memory version
  • Correct fast pointer null checks
  • Explains why restarting one pointer from the start finds the entry
  • Handles self-loops and immediately terminating chains
Key Topics to Cover
cycle detection
Floyd's algorithm
two pointers
linked lists
How to Approach This
  1. Clarify input constraints and edge cases before writing code.
  2. Walk through your approach verbally and confirm with the interviewer before coding.
  3. Start with a brute force solution, then optimize. Mention time and space complexity.
  4. Test your solution with examples, including edge cases like empty input or duplicates.
  5. Consider common patterns: sliding window, two pointers, hash map, BFS/DFS, dynamic programming.
Possible Follow-up Questions
  • How would you report the loop length too?
  • In a real crawler, why might you still prefer a hop limit over cycle detection?
  • How would you detect loops across all URLs in the map at once?
  • What if redirects depend on user agent, so the map is not fixed?
Sharpen Your Skills on Codemia

Practice similar problems with our interactive workspace, get AI feedback, and track your progress.

Practice DSA Problems
Sample Answer
Requirements

redirects maps a URL to its next hop; a URL with no entry is a final destination. From start, return the first URL on the path that belongs to a cycle, or None. Use O(1) extra space beyond the map...

High-Level Design

Treat redirects.get as the next pointer. Floyd's algorithm moves slow one hop and fast two hops; if the chain ends, fast reaches None first. If there is a loop, they must meet inside it. Let t...


Submit Your Answer
Markdown supported