Top 10 DSA Programming Questions for Coding Interviews

Prepare for coding interviews with these top 10 DSA programming questions. Practice most asked data structure and algorithm problems with simple explanations and solutions.

2 questionsDSABeginner
pythondsajava

Questions & Code Solutions

1Two Sum Problem – Find two numbers in an array that add up to a given target.

Easy

Problem Statement

Given an array of integers nums and an integer target, return the indices of the two numbers such that they add up to target. You may assume that each input would have exactly one solution, and you may not use the same element twice. You can return the answer in any order.


Example:

Input: nums = [2, 7, 11, 15], target = 9
Output: [0, 1]
Explanation: nums[0] + nums[1] = 2 + 7 = 9

Constraints:

  1. 2 ≤ nums.length ≤ 10⁴
  2. -10⁹ ≤ nums[i] ≤ 10⁹
  3. -10⁹ ≤ target ≤ 10⁹
  4. Only one valid answer exists

The Solution:

Use a hash map (dictionary) to store numbers you've seen along with their indices. For each number, check if the complement (target - current number) exists in the hash map. If it does, you've found your pair. If not, add the current number to the hash map and continue. This way, you only need to pass through the array once.

Steps:

  1. Create an empty hash map to store {value: index} pairs
  2. Loop through the array with index i
  3. Calculate complement = target - nums[i]
  4. Check if complement exists in the hash map
  5. If yes, return [hash_map[complement], i]
  6. If no, store nums[i] with its index in the hash map
  7. Continue to the next element

Time: O(n) - visit each element once

Space: O(n) - hash map stores up to n elements

python
def twoSum(nums, target): hash_map = {} for i in range(len(nums)): complement = target - nums[i] if complement in hash_map: return [hash_map[complement], i] hash_map[nums[i]] = i return [] # Test nums = [2, 7, 11, 15] target = 9 result = twoSum(nums, target) print(f"Output: {result}")

Output:

Output: [0, 1]

java
import java.util.HashMap; import java.util.Arrays; class Solution { public int[] twoSum(int[] nums, int target) { HashMap<Integer, Integer> map = new HashMap<>(); for (int i = 0; i < nums.length; i++) { int complement = target - nums[i]; if (map.containsKey(complement)) { return new int[] {map.get(complement), i}; } map.put(nums[i], i); } return new int[] {}; } public static void main(String[] args) { Solution sol = new Solution(); int[] nums = {2, 7, 11, 15}; int target = 9; int[] result = sol.twoSum(nums, target); System.out.println("Output: " + Arrays.toString(result)); } }

Output:

Output: [0, 1]

2Reverse a Linked List

Easy

Problem Statement

Given the head of a singly linked list (defined as ListNode), reverse the linked list and return the new head.

Example:

Input: 1 -> 2 -> 3 -> 4 -> 5 -> NULL
Output: 5 -> 4 -> 3 -> 2 -> 1 -> NULL

Constraints:

  1. The number of nodes is in the range [0, 5000].
  2. Node values are integers.

The Solution:

Use three pointers to flip the arrows one by one as you walk through the list. Start from the first node, save the next node so you don't lose it, flip the current node's arrow to point backwards, then move forward. Repeat this process until you reach the end. The last node you process becomes the new head of the reversed list.

Steps:

  1. Create three pointers: prev (starts as NULL), curr (starts at head), and next_temp
  2. While curr is not NULL, do the following: save curr.next in next_temp, make curr.next point to prev, move prev to curr, move curr to next_temp
  3. When curr becomes NULL, prev will be pointing to the new head
  4. Return prev as the new head of the reversed list

Time: O(n) - visit each node once

Space: O(1) - only use three pointers

python
class ListNode: def __init__(self, val=0, next=None): self.val = val self.next = next def reverseList(head): prev = None curr = head while curr: next_temp = curr.next curr.next = prev prev = curr curr = next_temp return prev def printList(head): result = [] while head: result.append(str(head.val)) head = head.next return " -> ".join(result) + " -> NULL" # Create: 1 -> 2 -> 3 -> 4 -> 5 -> NULL head = ListNode(1) head.next = ListNode(2) head.next.next = ListNode(3) head.next.next.next = ListNode(4) head.next.next.next.next = ListNode(5) print("Before:", printList(head)) head = reverseList(head) print("After:", printList(head))

Output:

Before: 1 -> 2 -> 3 -> 4 -> 5 -> NULL

After: 5 -> 4 -> 3 -> 2 -> 1 -> NULL

java
class ListNode { int val; ListNode next; ListNode(int val) { this.val = val; this.next = null; } } class Solution { public ListNode reverseList(ListNode head) { ListNode prev = null; ListNode curr = head; while (curr != null) { ListNode nextTemp = curr.next; curr.next = prev; prev = curr; curr = nextTemp; } return prev; } public static String printList(ListNode head) { StringBuilder sb = new StringBuilder(); while (head != null) { sb.append(head.val).append(" -> "); head = head.next; } sb.append("NULL"); return sb.toString(); } public static void main(String[] args) { ListNode head = new ListNode(1); head.next = new ListNode(2); head.next.next = new ListNode(3); head.next.next.next = new ListNode(4); head.next.next.next.next = new ListNode(5); System.out.println("Before: " + printList(head)); Solution sol = new Solution(); head = sol.reverseList(head); System.out.println("After: " + printList(head)); } }

Output:

Before: 1 -> 2 -> 3 -> 4 -> 5 -> NULL

After: 5 -> 4 -> 3 -> 2 -> 1 -> NULL