Foundation·Concept·2 min read

Arrays and strings in Java

Practice this question

Key operations

  • index access: arr[i]
  • length: arr.length
  • loops: for, while
  • string manipulation: substring, charAt, split, equals

Common Java questions

  • Two Sum
  • Best Time to Buy and Sell Stock
  • Maximum Subarray
  • Move Zeroes
  • Product of Array Except Self
  • Longest Substring Without Repeating Characters
  • Valid Palindrome
  • Group Anagrams

Java example: two sum

java
import java.util.*;

class Solution {
    public int[] twoSum(int[] nums, int target) {
        Map<Integer, Integer> map = new HashMap<>();

        for (int i = 0; i < nums.length; i++) {
            int needed = target - nums[i];
            if (map.containsKey(needed)) {
                return new int[] { map.get(needed), i };
            }
            map.put(nums[i], i);
        }
        return new int[] {};
    }
}

Time and space complexity

  • Time: O(n)
  • Space: O(n)

Adapted from 15_DSA_JAVA.md · “2. Arrays and strings in Java”. Code examples are study fragments. Difficulty and core labels are editorial estimates; version-specific behavior should be checked against your project.