Data Structures & AlgorithmsModerateInfosys · Cognizant pattern
What is the longest common prefix of the strings interview, internet, interval and intern?
- A. intern
- B. inter
- C. int
- D. interv
Correct answer: B. inter
Compare column by column: i, n, t, e, r are shared by all four. At index 5 the letters are v, n, v, n – they differ, so the prefix stops at 'inter'. 'intern' is common only to internet and intern.
Practise more Arrays, Strings, Linked Lists, Stacks, Queues and Hashing questionsArrays, Strings, Linked Lists, Stacks, Queues and Hashing: notes and key points
More Arrays, Strings, Linked Lists, Stacks, Queues and Hashing questions
- The first k = 4 elements of the queue 1, 2, 3, 4, 5, 6 (front first) are reversed using a stack: dequeue 4 items onto the stack, pop them…
- k sorted lists contain N elements in total. They are merged into one sorted list by repeatedly taking the smallest head from a min-heap…
- A deque of indices gives the MINIMUM of every window of size 3 in [4, 2, 12, 3, 8, 1, 7]. What are the window minima?
- A hash table of size 10 uses linear probing. Slots 2, 3, 4, 7 and 8 are occupied and all others are empty (no deletions have happened). An…
- Quickselect with Lomuto partition (pivot = last element) looks for the 3rd smallest element of [7, 2, 9, 4, 3, 8], recursing only into the…
- Binary search finds the index of the minimum of the rotated sorted array [15, 18, 22, 3, 6, 9, 12] using: mid = (lo + hi) / 2; if a[mid] >…