DSA - Arrays and Strings

0.0(0)
studied byStudied by 0 people
learnLearn
examPractice Test
spaced repetitionSpaced Repetition
heart puzzleMatch
flashcardsFlashcards
Card Sorting

1/68

encourage image

There's no tags or description

Looks like no tags are added yet.

Study Analytics
Name
Mastery
Learn
Test
Matching
Spaced

No study sessions yet.

69 Terms

1
New cards

What is an array?

A data structure storing a collection of elements of the same type in contiguous memory, accessible by index.

2
New cards

What is a string?

A sequence of characters, often conceptually an array of characters. Immutable in languages like Python and Java.

3
New cards

Array: Time complexity for accessing an element by index?

O(1)

4
New cards

Array: Time complexity for inserting/deleting in the middle?

O(N)

5
New cards

String: Time complexity for concatenation (creating new string)?

O(N+M) where N and M are string lengths.

6
New cards

What is the Two Pointers technique?

Uses two integer indices to traverse an array/string, often from different ends or at different speeds, to process pairs or find conditions.

7
New cards

When should you use Two Pointers?

Reversing arrays/strings, finding pairs in sorted arrays, palindrome detection, partitioning.

8
New cards

What is the Sliding Window technique?

Maintains a subsegment (window) that slides over data to find a part satisfying certain properties (e.g., max sum subarray, longest substring with k distinct chars).

9
New cards

When should you use Sliding Window?

Problems asking for max/min sum/average of a fixed/variable size subarray/substring, or longest/shortest substring with certain properties.

10
New cards

What is a Hash Map (or Hash Set)?

Data structure for O(1) average time key-value (map) or key (set) storage, lookup, and deletion. Useful for counting, existence checks.

11
New cards

When should you use a Hash Map/Set?

Counting frequencies, checking for duplicates, Two Sum type problems, anagrams.

12
New cards

What is the Prefix Sum technique?

Precalculating sums (or products, XORs, etc.) up to each index P[i] = arr[0] + … + arr[i], allowing O(1) range sum queries (P[k] - P[j-1]).

13
New cards

When should you use Prefix Sum?

Frequent range sum queries, finding equilibrium/pivot indices, problems involving sums/products to left/right of an element.

14
New cards

What is often a crucial preprocessing step for array problems, enabling techniques like two-pointers or binary search?

Sorting the array.

15
New cards

Two Sum (LeetCode): Problem Summary

Given an array of integers nums and an integer target, return indices of the two numbers such that they add up to target.

16
New cards

Two Sum: Optimal technique?

Hash Map.

17
New cards

Two Sum: How to recognize Hash Map applies?

Need to find two numbers summing to a target; hash map allows O(1) lookup for the complement.

18
New cards

Two Sum: Time/Space Complexity (Hash Map)?

Time: O(N), Space: O(N).

19
New cards

Best Time to Buy and Sell Stock: Problem Summary

Given stock prices over days, find max profit from one buy and one later sell.

20
New cards

Best Time to Buy and Sell Stock: Key technique?

One Pass (Kadane's variant): track min price so far and calculate max profit.

21
New cards

Best Time to Buy and Sell Stock: Time/Space Complexity?

Time: O(N), Space: O(1).

22
New cards

Contains Duplicate: Problem Summary

Return true if any value appears at least twice in an array, false otherwise.

23
New cards

Contains Duplicate: Key technique(s)?

Hash Set or Sorting.

24
New cards

Contains Duplicate: How to recognize Hash Set applies?

Need to efficiently check for existence of elements seen before.

25
New cards

Contains Duplicate: Time/Space (Hash Set)?

Time: O(N), Space: O(N).

26
New cards

Contains Duplicate: Time/Space (Sorting)?

Time: O(NlogN), Space: O(1) or O(N) depending on sort.

27
New cards

Valid Anagram: Problem Summary

Given two strings s and t, return true if t is an anagram of s.

28
New cards

Valid Anagram: Key technique(s)?

Hash Map (character counts) or Sorting.

29
New cards

Valid Anagram: How to recognize Hash Map applies?

Problem involves comparing character frequencies.

30
New cards

Valid Anagram: Time/Space (Hash Map)?

Time: O(N), Space: O(K) (K=alphabet size, so effectively O(1)).

31
New cards

Valid Anagram: Time/Space (Sorting)?

Time: O(NlogN), Space: O(N).

32
New cards

Longest Substring Without Repeating Characters: Problem Summary

Find the length of the longest substring in s without repeating characters.

33
New cards

Longest Substring Without Repeating Characters: Key technique?

Sliding Window with Hash Set/Map.

34
New cards

Longest Substring Without Repeating Characters: How to recognize Sliding Window applies?

Looking for "longest substring" with a property ("without repeating characters"). Hash set/map tracks chars in window.

35
New cards

Longest Substring Without Repeating Characters: Time/Space?

Time: O(N), Space: O(K) (K=alphabet size or min(N, alphabet size)).

36
New cards

Minimum Size Subarray Sum: Problem Summary

Given nums and target, find minimal length of a contiguous subarray whose sum is >= target.

37
New cards

Minimum Size Subarray Sum: Key technique?

Sliding Window (variable size).

38
New cards

Minimum Size Subarray Sum: How to recognize Sliding Window applies?

"Minimal length" of "contiguous subarray" with a sum condition.

39
New cards

Minimum Size Subarray Sum: Time/Space?

Time: O(N), Space: O(1).

40
New cards

Reverse String: Problem Summary

Reverse an array of characters in-place.

41
New cards

Reverse String: Key technique?

Two Pointers.

42
New cards

Reverse String: How to recognize Two Pointers apply?

Explicit request for in-place reversal of a sequence.

43
New cards

Reverse String: Time/Space?

Time: O(N), Space: O(1).

44
New cards

In Python, how can you efficiently build a string from a list of characters or smaller strings?

Use "".join(listofstrings).

45
New cards

Why is s += char in a loop inefficient for building strings in Python/Java?

Strings are immutable, so each += creates a new string object, leading to O(N^2) complexity for N appends.

46
New cards

General pattern for Sliding Window (variable size): Expand window by adding element at right.

While condition met/violated: update result, shrink window by removing element at left, move left. Move right.

47
New cards

General pattern for Two Pointers (opposite ends): left = 0, right = len-1.

While left < right: process arr[left], arr[right]; move left and/or right based on logic.

48
New cards

What does 'contiguous memory locations' mean for an array?

Elements are stored one after another in memory, without gaps, allowing for pointer arithmetic and efficient indexed access.

49
New cards

What does 'immutable string' mean?

Once a string object is created, its content (sequence of characters) cannot be changed. Operations that seem to modify it actually create a new string object.

50
New cards
What is an array?
Linear data structure of same-type elements in contiguous memory locations enabling O(1) access.
51
New cards
What is a string?
A sequence of characters implemented as a character array with encoding and possible immutability.
52
New cards
When to use a hash map?
When you need O(1) average-time key→value lookups, e.g., Two Sum.
53
New cards
Two Pointers technique
Use two indices (often ends) to scan for pairs or shrink/expand windows, e.g., Container With Most Water.
54
New cards
What is the sliding window technique?
Maintain a moving subarray/substring window and update results incrementally instead of recomputing.
55
New cards
What is prefix sum?
Precompute cumulative sums so any subarray sum = prefix[j] - prefix[i-1].
56
New cards
Two Sum
Optimal technique: Hash Map.
57
New cards
3Sum
Optimal technique: Sorting + Two Pointers.
58
New cards
Longest Substring Without Repeating Characters
Optimal technique: Sliding Window + Hash Map.
59
New cards
Minimum Window Substring
Optimal technique: Sliding Window + Hash Map.
60
New cards
Product of Array Except Self
Optimal technique: Prefix & Suffix Product.
61
New cards
Best Time to Buy and Sell Stock
Optimal technique: One-pass min tracking.
62
New cards
Maximum Subarray
Optimal technique: Kadane’s algorithm (DP).
63
New cards
Search in Rotated Sorted Array
Optimal technique: Modified Binary Search.
64
New cards
Valid Anagram
Optimal technique: Hash Map character counts.
65
New cards
Implement strStr()
Optimal technique: KMP (O(n+m)).
66
New cards
Palindrome Partitioning
Optimal technique: DFS + Backtracking + DP memo.
67
New cards
Container With Most Water recognition clue
“Max area,” two lines, width*min height → Two Pointers.
68
New cards
Longest Palindromic Substring recognition cue
“Longest palindrome,” center expansion or Manacher’s.
69
New cards