LearnFrenzy
Sign in Take a free test

DSA & Algorithms · Topic 2 of 13

Arrays and Strings: questions with answers

Indexing and memory layout, prefix sums, two pointers, sliding window, Kadane, rotation, hashing tricks, KMP and the string pitfalls of C, C++, Java and Python.

Questions
45
Timed sets
3
Answers run or computed
41
Your accuracy
–

What this topic covers

29 subtopics
  • Two pointers5
  • 2D arrays3
  • Prefix sums3
  • C strings2
  • Character codes2
  • Common bugs2
  • Indexing2
  • Kadane's algorithm2
  • Palindromes2
  • Rotation2
  • Sliding window2
  • Anagrams1
Show all 29 subtopics
  • Bit tricks1
  • Counting characters1
  • Equality1
  • Hashing1
  • Immutability1
  • Initialisation1
  • Intervals1
  • KMP1
  • Memory layout1
  • Operations and cost1
  • Permutations1
  • Prefix products1
  • Reversal1
  • Searching1
  • String matching1
  • Voting algorithm1
  • Word manipulation1
  • Beginner 15
  • Intermediate 15
  • Advanced 15

About this topic

Arrays and strings are the first round of almost every coding interview. Most questions reduce to a handful of patterns: prefix sums for range queries, two pointers on sorted data, a sliding window for subarrays, a hash map to remember what you have seen, and Kadane for maximum subarrays. Watch the language traps too – Java strings are immutable, == compares references, and a Python list multiplied by 3 copies references, not rows. Every output on this page comes from running the code.

Free preview

Sample questions

Try each one first, then open the answer for the explanation and why the other options are wrong.

Question 1 Indexing Beginner Practice Answer computed

Using the array shown, what is the value of a[a[1]]?

Array
a
  1. 03
  2. 10
  3. 24
  4. 31
  5. 42

The answer is a number – work it out, then open the solution.

Show answer and explanation
Correct answer 3

Explanation

Work from the inside out: a[1] is 0, so a[a[1]] is a[0], which is 3. Indices start at 0.

Question 2 Prefix sums Beginner Practice Answer computed

What is the prefix-sum array p of this array, where p[i] = a[0] + … + a[i]?

Array
a
  1. 03
  2. 11
  3. 24
  4. 31
  5. 45
  1. 3
  2. 1
  3. 4
  4. 1
  5. 5
  1. 3
  2. 4
  3. 5
  4. 5
  5. 6
  1. 14
  2. 11
  3. 10
  4. 6
  5. 5
  1. 3
  2. 4
  3. 8
  4. 9
  5. 14
Show answer and explanation
Correct answer D

Explanation

Each entry adds the next element to the previous total: 3, 3 + 1, 4 + 4, 8 + 1, 9 + 5. With a prefix array, any range sum is one subtraction.

Why the other options are wrong

  • AThat is the original array.
  • BEach entry adds the running total, not just the previous element.
  • CThose are suffix sums.
Question 3 Kadane's algorithm Intermediate Practice Answer computed

What is the largest sum of a contiguous (non-empty) subarray?

Array
a
  1. 0-2
  2. 11
  3. 2-3
  4. 34
  5. 4-1
  6. 52
  7. 61
  8. 7-5
  9. 84

The answer is a number – work it out, then open the solution.

Show answer and explanation
Correct answer 6

Explanation

The best subarray is [4, −1, 2, 1] = 6. Kadane's algorithm finds it in one pass: at each element, either extend the current run or start a new one there – whichever is larger – and remember the best run seen.

Question 4 Two pointers Advanced Practice Answer computed

The array gives bar heights of width 1. How many units of rain water are trapped between the bars?

Bar heights
h
  1. 00
  2. 11
  3. 20
  4. 32
  5. 41
  6. 50
  7. 61
  8. 73
  9. 82
  10. 91
  11. 102
  12. 111

The answer is a number – work it out, then open the solution.

Show answer and explanation
Correct answer 6

Explanation

Water above bar i is min(highest bar on its left, highest on its right) − h[i]. Summing over all bars gives 1 + 1 + 2 + 1 + 1 = 6. The two-pointer version computes this in O(n) time and O(1) space.

41 more questions in the timed setsSame format as the samples, with a timer, a question palette and a full review at the end.
Start practising

DSA & Algorithms

All 13 topics

Topics, levels and full mocks
  1. 01 Time and Space Complexity45 questions · 3 levels
  2. 02 Arrays and Strings45 questions · 3 levels
  3. 03 Linked Lists45 questions · 3 levels
  4. 04 Stacks and Queues45 questions · 3 levels
  5. 05 Hashing45 questions · 3 levels
  6. 06 Recursion and Backtracking45 questions · 3 levels
  7. 07 Sorting and Searching45 questions · 3 levels
  8. 08 Trees and BST45 questions · 3 levels
  9. 09 Heaps and Priority Queues45 questions · 3 levels
  10. 10 Graphs45 questions · 3 levels
  11. 11 Dynamic Programming45 questions · 3 levels
  12. 12 Greedy and Bit Manipulation45 questions · 3 levels
  13. 13 GATE-Style DSA & Algorithms Questions (MSQ & NAT)45 questions · 3 levels

Search LearnFrenzy

Esc

Quick links