Data Structures & AlgorithmsHardCodeforces Div2 B · DP warm-up
Problem: Floyd's cycle detection ('tortoise and hare') finds whether a linked list has a cycle. What are its time and space complexity?
- A. O(log n) time, O(1) space
- B. O(n) time, O(n) space (needs a visited-set)
- C. O(n^2) time, O(1) space
- D. O(n) time, O(1) space
Correct answer: D. O(n) time, O(1) space
Two pointers moving at different speeds are guaranteed to meet inside a cycle within O(n) steps, using only two pointer variables -- no extra data structure, so O(1) space.
More Coding Interview Problems questions
- Problem: What is the time complexity of a segment tree's range-query and point-update operations on an array of size n?
- Problem: Finding the median of two sorted arrays of sizes m and n can be done faster than merging them. What is the best known time…
- Problem: Which statement about the backtracking solution to generating all subsets of an n-element set is correct?
- Problem: Generating all permutations of an n-element array via backtracking takes how much time, and why?
- Problem: In the N-Queens backtracking search, what pruning technique keeps the search from degenerating into a full O(N^N) brute force?
- Problem: Which best explains why a monotonic stack is the standard technique for the 'next greater element' problem, giving O(n) overall…