Data Structures & AlgorithmsEasyCognizant · Capgemini pattern
A stack is implemented with a singly linked list. Where should push and pop operate to keep both O(1)?
- A. At the tail – append and remove the tail
- B. Push at the head, pop at the tail
- C. At the head – insert a new head and remove the head
- D. Push at the tail, pop at the head
Correct answer: C. At the head – insert a new head and remove the head
Head insertion and head deletion are both pointer updates. Removing the tail needs the predecessor (O(n)), so any design that pops at the tail is O(n). Push at tail/pop at head is a queue, not a stack.
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
- What is printed? (C strings) c char s[] = "Placement"; printf("%zu %zu", strlen(s), sizeof(s));
- What is printed? (pointer arithmetic on an int array) c int a[] = {10, 20, 30, 40, 50}; int p = a + 1; printf("%d ", (p + 2)); printf("%d…
- What is printed? (2-D array in row-major order) python R, C = 3, 4 base, size = 1000, 4 def addr(i, j, rowmajor=True): return base + size…
- What is printed? (Python string immutability and methods) python s = "Hello World" t = s.replace("o", "0", 1) u = s.swapcase()[::2]…
- What is printed? (queue simulation – hot potato with step k) python from collections import deque q = deque(range(1, 8)); out = [] while…
- What is printed? (Josephus recurrence) python def josephus(n, k): r = 0 for m in range(2, n + 1): r = (r + k) % m return r + 1…