0% found this document useful (0 votes)
39 views8 pages

DSA Questions and Solutions

Uploaded by

shreyatyrtrtrtrt
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
39 views8 pages

DSA Questions and Solutions

Uploaded by

shreyatyrtrtrtrt
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

Data Structures and Algorithms (DSA) Questions and Answers

Section 1: Arrays

1. Find the maximum sum of a subarray (Kadane's Algorithm)

- Problem: Given an array of integers, find the contiguous subarray with the maximum sum.

- Brute Force:

- Iterate over all subarrays and calculate their sums.

- Time Complexity: O(n^2)

- Space Complexity: O(1)

- Optimal Solution:

- Use Kadane's algorithm: iterate through the array while maintaining the maximum sum.

- Time Complexity: O(n)

- Space Complexity: O(1)

- Edge Cases:

- All negative numbers.

- Single element array.

- Follow-ups:

- Find the subarray itself.

- Handle circular arrays.

2. Find the next permutation

- Problem: Implement the next permutation algorithm for a sequence of numbers.

- Brute Force:

- Generate all permutations and sort them.

- Time Complexity: O(n! * n)


- Space Complexity: O(n!)

- Optimal Solution:

- Identify the pivot and swap with the next larger element, then reverse the suffix.

- Time Complexity: O(n)

- Space Complexity: O(1)

- Edge Cases:

- Already the largest permutation.

- Single element array.

- Follow-ups:

- Find the k-th permutation.

Section 2: Strings

3. Longest Palindromic Substring

- Problem: Find the longest palindromic substring in a given string.

- Brute Force:

- Check all substrings for being palindromes.

- Time Complexity: O(n^3)

- Space Complexity: O(1)

- Optimal Solution:

- Use dynamic programming or expand around the center.

- Time Complexity: O(n^2)

- Space Complexity: O(n^2) or O(1) (expand center).

- Edge Cases:

- Empty string.

- All characters are the same.

- Follow-ups:
- Return all palindromic substrings.

4. Implement Atoi (String to Integer)

- Problem: Convert a string to an integer.

- Brute Force:

- Parse each character and construct the integer.

- Time Complexity: O(n)

- Space Complexity: O(1)

- Optimal Solution:

- Handle whitespace, sign, overflow, and invalid characters systematically.

- Time Complexity: O(n)

- Space Complexity: O(1)

- Edge Cases:

- Strings with non-numeric characters.

- Overflow/underflow.

Section 3: Trees

5. Binary Tree Inorder Traversal

- Problem: Perform inorder traversal of a binary tree.

- Brute Force:

- Use recursion.

- Time Complexity: O(n)

- Space Complexity: O(h) (call stack).

- Optimal Solution:

- Use an iterative approach with a stack or Morris traversal.

- Time Complexity: O(n)


- Space Complexity: O(h) or O(1) (Morris).

- Edge Cases:

- Empty tree.

- Single-node tree.

- Follow-ups:

- Postorder traversal.

6. Lowest Common Ancestor of a Binary Tree

- Problem: Find the lowest common ancestor (LCA) of two nodes in a binary tree.

- Brute Force:

- Find paths to both nodes and compare.

- Time Complexity: O(n)

- Space Complexity: O(h)

- Optimal Solution:

- Recursively check for nodes in left and right subtrees.

- Time Complexity: O(n)

- Space Complexity: O(h)

- Edge Cases:

- One or both nodes do not exist.

- Nodes are the same.

Section 4: Greedy Algorithms

7. Activity Selection Problem

- Problem: Select the maximum number of activities that don't overlap.

- Brute Force:

- Test all subsets for overlap.


- Time Complexity: O(2^n)

- Space Complexity: O(1)

- Optimal Solution:

- Sort activities by finish time and iteratively select.

- Time Complexity: O(n log n)

- Space Complexity: O(1)

- Edge Cases:

- All activities overlap.

- Single activity.

8. Huffman Encoding

- Problem: Build a Huffman Tree for data compression.

- Approach:

- Use a priority queue to combine the smallest frequencies iteratively.

- Time Complexity: O(n log n)

- Space Complexity: O(n)

- Edge Cases:

- Single character input.

- Uniform frequency distribution.

Section 5: Dynamic Programming

9. Longest Increasing Subsequence

- Problem: Find the length of the longest increasing subsequence.

- Brute Force:

- Generate all subsequences and test.

- Time Complexity: O(2^n)


- Space Complexity: O(n)

- Optimal Solution:

- Use DP or binary search with a list.

- Time Complexity: O(n log n)

- Space Complexity: O(n)

- Edge Cases:

- Strictly decreasing sequence.

- All elements the same.

10. 0/1 Knapsack Problem

- Problem: Maximize the value of items that can fit into a knapsack of given capacity.

- Brute Force:

- Test all subsets of items.

- Time Complexity: O(2^n)

- Space Complexity: O(n)

- Optimal Solution:

- Use a DP table.

- Time Complexity: O(n * W) (W is knapsack capacity).

- Space Complexity: O(W)

- Edge Cases:

- Items heavier than capacity.

- Multiple optimal solutions.

Section 6: Binary Search

11. Find the Peak Element

- Problem: Find any peak element in an array.


- Brute Force:

- Traverse the array to find a local maximum.

- Time Complexity: O(n)

- Space Complexity: O(1)

- Optimal Solution:

- Use binary search.

- Time Complexity: O(log n)

- Space Complexity: O(1)

- Edge Cases:

- Multiple peaks.

- Array of length 1.

12. Search in Rotated Sorted Array

- Problem: Search for a target in a rotated sorted array.

- Brute Force:

- Linearly search for the target.

- Time Complexity: O(n)

- Space Complexity: O(1)

- Optimal Solution:

- Use modified binary search.

- Time Complexity: O(log n)

- Space Complexity: O(1)

- Edge Cases:

- Target not present.

- 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

follow-ups, download the attached PDF below.

You might also like