DSA Questions and Solutions
DSA Questions and Solutions
Section 1: Arrays
- Problem: Given an array of integers, find the contiguous subarray with the maximum sum.
- Brute Force:
- Optimal Solution:
- Use Kadane's algorithm: iterate through the array while maintaining the maximum sum.
- Edge Cases:
- Follow-ups:
- Brute Force:
- Optimal Solution:
- Identify the pivot and swap with the next larger element, then reverse the suffix.
- Edge Cases:
- Follow-ups:
Section 2: Strings
- Brute Force:
- Optimal Solution:
- Edge Cases:
- Empty string.
- Follow-ups:
- Return all palindromic substrings.
- Brute Force:
- Optimal Solution:
- Edge Cases:
- Overflow/underflow.
Section 3: Trees
- Brute Force:
- Use recursion.
- Optimal Solution:
- Edge Cases:
- Empty tree.
- Single-node tree.
- Follow-ups:
- Postorder traversal.
- Problem: Find the lowest common ancestor (LCA) of two nodes in a binary tree.
- Brute Force:
- Optimal Solution:
- Edge Cases:
- Brute Force:
- Optimal Solution:
- Edge Cases:
- Single activity.
8. Huffman Encoding
- Approach:
- Edge Cases:
- Brute Force:
- Optimal Solution:
- Edge Cases:
- Problem: Maximize the value of items that can fit into a knapsack of given capacity.
- Brute Force:
- Optimal Solution:
- Use a DP table.
- Edge Cases:
- Optimal Solution:
- Edge Cases:
- Multiple peaks.
- Array of length 1.
- Brute Force:
- Optimal Solution:
- Edge Cases:
- Single-element array.
---
This document includes 12 sample questions and solutions from each section of DSA. For the full
list of 100 questions and their detailed solutions with code implementations, edge cases, and