Data Structures & AlgorithmsEasyWipro · Accenture pattern
What is printed? (stack-based reversal of words)
What is printed? (stack-based reversal of words)
s = "data structures are fun"
st = []
for w in s.split():
st.append(w)
out = []
while st:
out.append(st.pop()[::-1])
print(" ".join(out))- A. fun are structures data
- B. nuf era serutcurts atad
- C. atad serutcurts era nuf
- D. nuf era structures data
Correct answer: B. nuf era serutcurts atad
Popping the stack reverses the word order (fun, are, structures, data), and [::-1] reverses each word's letters, so the whole string is reversed: "nuf era serutcurts atad".
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
- Convert the infix expression to postfix (^ is right-associative, higher than and /, which are higher than + and −). What is printed?…
- Evaluate the postfix expression with single-digit operands. What is printed? python st = [] for t in "62/3-42+": if t.isdigit()…
- What is printed? (deque operations) python from collections import deque d = deque([1, 2, 3]) d.appendleft(0); d.append(4) d.rotate(2)…
- What is printed? (singly linked list, delete every node whose value equals x) python class N: def init(s, v, n=None): s.v, s.n = v, n h =…
- What is printed? (finding the intersection node of two linked lists by switching heads) python class N: def init(s, v, n=None): s.v, s.n =…
- What is printed? (Java String vs StringBuilder) java String s = "ab"; s.concat("cd"); StringBuilder sb = new StringBuilder("ab")…