Data Structures & AlgorithmsModerateTCS NQT · number theory
Problem: What is the complexity of checking whether a number n is prime by trial division up to sqrt(n)?
- A. O(log n)
- B. O(n)
- C. O(sqrt n)
- D. O(n log n)
Correct answer: C. O(sqrt n)
Trial division up to sqrt(n) takes about sqrt(n) divisions, which is fast for n up to 10^12.
More Coding Interview Problems questions
- Problem: Edges of an undirected graph on vertices 0–4 are processed in the given order: e1 = 0-1, e2 = 1-2, e3 = 2-3, e4 = 3-4, e5 = 4-0…
- Problem: How many spanning trees does the cycle graph C5 (5 vertices in one cycle) have?
- Problem: A directed graph on vertices 0–5 has arcs 0→1, 1→2, 2→0, 2→3, 3→4, 4→3, 4→5, 5→1. How many vertices have in-degree equal to…
- Problem: On the undirected graph with edges 0-1 (4), 0-2 (1), 2-1 (2), 1-3 (5), 2-3 (9), 3-4 (3), find the minimum number of edges used by…
- Problem: The rooted tree (root 1) has children 1: [2, 3], 2: [4, 5], 3: [6], 4: [7], 6: [8]. How many nodes have a depth divisible by 3…
- Problem: In the string "abcbaxyzzyx", count the palindromic substrings of length exactly 5.