Find cycle in string

Last updated: August 20, 2025

Quick Overview

Given a string, determine if it contains a cycle by checking if any substring can be repeated to form the entire string. The input will be a single string, and the output should be a boolean value indicating whether a cycle exists. The solution should be efficient, ideally with a time complexity of O(n).

Twitter/X
Coding & Algorithms
Software Engineer
Twitter/X
August 20, 2025
Software Engineer
Onsite
Coding & Algorithms
Medium

12

17

4,384 solved


Given a string, determine if it contains a cycle by checking if any substring can be repeated to form the entire string. The input will be a single string, and the output should be a boolean value indicating whether a cycle exists. The solution should be efficient, ideally with a time complexity of O(n).

Coding interviews at Twitter/X focus on problem-solving approach as much as the final solution. The interviewer wants to see you break down the problem, consider edge cases, and optimize iteratively. Communication throughout the process is key.

What the Interviewer Expects
  • Recognize the underlying problem pattern (sliding window, two pointers, BFS/DFS, etc.)
  • Discuss multiple approaches and trade-offs before coding
  • Implement an optimal solution with clean, production-quality code
  • Handle all edge cases including boundary conditions and invalid input
  • Optimize both time and space complexity with clear justification
  • Test your solution systematically with well-chosen examples
Key Topics to Cover
Sorting and searching
Hash maps and frequency counting
Edge cases and input validation
Time and space complexity analysis
Tree structures and recursion
Dynamic programming and memoization
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 modify your solution to handle streaming input?
  • How would you test this solution thoroughly?
  • How would your solution change if the input was sorted?
  • How would you parallelize this solution?
Sharpen Your Skills on Codemia

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

Practice DSA Problems
Sample Answer
Problem Analysis

To determine if a string contains a cycle by checking if any substring can be repeated to form the entire string, we can leverage the concept of string manipulation and pattern recognition. Specifical...

Approach
  1. Calculate the Prefix Function: We will create an array lps (longest prefix which is also suffix) that will store the lengths of the longest proper prefix which is also a suffix for substrings...

Submit Your Answer
Markdown supported

Related Questions