Data Structures & AlgorithmsModerateAccenture · Cognizant coding round pattern
Problem: n ≤ 10⁵ points on a line; answer 'how many points lie in [l, r]?' for 10⁵ queries. What is the simplest efficient solution?
- A. A hash set of points – O(1) per query
- B. Scan all points per query – O(n) each is fine
- C. Sort the points once; each query is bisect_right(r) − bisect_left(l) – O((n + q) log n)
- D. A max-heap of points
Correct answer: C. Sort the points once; each query is bisect_right(r) − bisect_left(l) – O((n + q) log n)
Two binary searches on the sorted array give the count. A hash set answers membership, not ranges. Scanning is 10¹⁰ in total.
More Coding Interview Problems questions
- Problem: Implement a queue using two stacks and report the dequeued values. python class Q: def init(self): self.inb, self.out = [], []…
- Problem: Evaluate a basic calculator expression with +, −, parentheses and spaces. python def calc(s): res, num, sign, st = 0, 0, 1, []…
- Problem: Decode a string like 3[a2[c]] where k[...] repeats the bracket content k times. python def decode(s): st, cur, k = [], '', 0 for…
- Problem: For each element, find the next greater element in a circular array (−1 if none). python def ngecircular(a): n = len(a); res =…
- Problem: Compute the stock span: for each day, the number of consecutive days up to today with price ≤ today's price. python def spans(p)…
- Problem: Return the k most frequent words, ties broken alphabetically. python from collections import Counter def topk(words, k): c =…