0% found this document useful (0 votes)
89 views3 pages

DSA Lab Exam Coding Questions

The document lists various DSA lab exam questions covering a range of topics including array manipulation, string processing, matrix operations, and algorithm implementation. Each question presents a specific problem to solve, such as finding common elements, sorting arrays, or checking for palindromes. The questions are designed to test the understanding and application of data structures and algorithms.

Uploaded by

tonoydas2025
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)
89 views3 pages

DSA Lab Exam Coding Questions

The document lists various DSA lab exam questions covering a range of topics including array manipulation, string processing, matrix operations, and algorithm implementation. Each question presents a specific problem to solve, such as finding common elements, sorting arrays, or checking for palindromes. The questions are designed to test the understanding and application of data structures and algorithms.

Uploaded by

tonoydas2025
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

DSA Lab Exam Questions

1. Find common elements in three sorted arrays.


2. Find a triplet that sums to a given value.
3. Find whether an array is a subset of another array.
4. First element to occur k times.
5. Reverse words in a given string.
6. Given an expression string exp, write a program to examine whether the pairs
and the orders of “{“, “}”, “(“, “)”, “[“, “]” are correct in the given expression.
7. Given an array of N integers where each value represents the number of
chocolates in a packet. Each packet can have a variable number of chocolates.
There are m students, the task is to distribute chocolate packets such that:
Each student gets one packet.
The difference between the number of chocolates in the packet
with maximum chocolates and the packet with minimum chocolates given to
the students is minimum.
Examples:
Input : arr[] = {7, 3, 2, 4, 9, 12, 56} , m = 3
Output: Minimum Difference is 2
Explanation:
We have seven packets of chocolates and we need to pick three packets for 3
students
If we pick 2, 3 and 4, we get the minimum difference between maximum and
minimum packet sizes.
Input : arr[] = {3, 4, 1, 9, 56, 7, 9, 12} , m = 5
Output: Minimum Difference is 6
Input : arr[] = {12, 4, 7, 9, 2, 23, 25, 41, 30, 40, 28, 42, 30, 44, 48, 43, 50} , m =
7
Output: Minimum Difference is 10
8. Split the array into two parts with equal sum.
9. Check if the array can be divided into pairs whose sum is divisible by k.
10. Merge two sorted arrays.
11. Check if an array is a palindrome.
12. Segregate even and odd nodes in an array.
13. QuickSort code.
14. Sort an array of strings by the number of vowels.
15. Sort an array of strings by their reversed forms.
16. Sort an array of integers by their frequency.
17. Sort an array using merge sort.
18. Sort an array of elements such that all odd numbers appear before even
numbers.
19. Sort an array of names based on their lengths.
20. Check if two strings are anagrams.
21. Print all permutations of a string.
22. Convert a string to lowercase.
23. Count the number of occurrences of a substring in a string.
24. Given a matrix, return its transpose.
25. Rotate a given square matrix by 90 degrees clockwise.
26. Given a matrix where every row is sorted in increasing order from left to right,
and every column is sorted in increasing order from top to bottom, write a
program that searches for a target value.
27. Given an m x n matrix, if an element is 0, set its entire row and column to 0.
28. Given a 2D matrix, return all elements in a diagonal order.
29. WAP to multiply 2 matrices.
30. Given an array nums of n integers, return an array output such that output[i] is
equal to the product of all the elements of nums except nums[i].
31. Given an array where the ith element is the price of a given stock on day i, find
the maximum profit you can achieve. You may complete at most one
transaction.
Example:
Input: [7,1,5,3,6,4]
Output: 5 (buy on day 2 at price 1 and sell on day 5 at price 6)
32. Given a string s, find the longest palindromic substring in s.
Example:
Input: "babad"
Output: "bab" (or "aba")
33. Implement a method to perform basic string compression using the counts of
repeated characters. If the compressed string would not become smaller than
the original string, return the original string.
Example:
Input: "aabcccccaaa"
Output: "a2b1c5a3"
34. Given two strings, determine if they are anagrams of each other.
35. Given a string s, return True if the string can be made a palindrome after
deleting at most one character.
Example:
Input: "abca"
Output: True (You could delete the character "c".)
36. Convert a Roman numeral to an integer.
37. The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of
rows like this: (you may want to display this pattern in a fixed font for better
legibility)
P A H N
APLSIIG
Y I R
And then read line by line: "PAHNAPLSIIGYIR"
Write the code that will take a string and make this conversion given a number
of rows:
string convert(string s, int numRows);
38. Given a string containing digits from 2-9 inclusive, return all possible letter
combinations that the number could represent. Return the answer in any
order.
A mapping of digits to letters (just like on the telephone buttons) is given
below. Note that 1 does not map to any letters.
Example 1:

Input: digits = "23"


Output: ["ad","ae","af","bd","be","bf","cd","ce","cf"]
Example 2:

Input: digits = ""


Output: []
Example 3:

Input: digits = "2"


Output: ["a","b","c"]
Constraints:

0 <= [Link] <= 4


digits[i] is a digit in the range ['2', '9'].

39. Given a string s, reverse only all the vowels in the string and return it.
The vowels are 'a', 'e', 'i', 'o', and 'u', and they can appear in both lower and
upper cases, more than once.
Example 1:
Input: s = "hello"
Output: "holle"
Example 2:
Input: s = "leetcode"
Output: "leotcede"
40. Given two non-negative integers, num1 and num2 represented as string, return
the sum of num1 and num2 as a string.
Example 1:
Input: num1 = "11", num2 = "123"
Output: "134"
Example 2:
Input: num1 = "456", num2 = "77"
Output: "533"
Example 3:
Input: num1 = "0", num2 = "0"
Output: "0"
41. Insert at beginning of an array.
42. Insert at nth position in an array.

Common questions

Powered by AI

Two strings are anagrams if they can be rearranged to become identical. To determine this, you can convert both strings to arrays of characters, sort these arrays, and compare them for equality. Alternatively, you can count the frequency of each character in both strings using a hash map or count array; if the frequency distributions match, the two strings are anagrams .

A string can potentially become a palindrome by removing a single character if, after removal, the rest of the string or any contiguous substring is a palindrome. Start by checking for mismatches from the beginning and end of the string; once a mismatch is found, check if removing either character forms the remainder into a palindrome. This check is best handled using a two-pointer technique that verifies sub-string integrity .

Compress a string by traversing it once, counting consecutive repeating characters, and appending each character followed by its count to a new string. If the generated string is not shorter than the original, return the original string to avoid increasing the size. This approach might not reduce the size if, for example, there are many distinct characters or short sequences, resulting in an output string longer than the input .

To find all permutations of a string, a backtracking algorithm can be employed. The process involves fixing one character at a time and recursively swapping it with each of the subsequent characters, then fixing the next and proceeding likewise. This generates permutations by swapping backtracked characters back to their original positions at each step to explore new permutations .

The longest palindromic substring can be found using dynamic programming or the expand-around-center approach. In dynamic programming, you create a table where each entry at i, j determines if the substring from i to j is a palindrome, filling this table based on whether the ends match and the inner substring is a palindrome. The expand-around-center method considers each character and each pair of consecutive characters as potential centers, expanding outward as long as a palindrome is detected. Both methods yield a time complexity of O(n^2).

An array is a palindrome if it reads the same forwards and backwards. To determine this, compare pairs of elements starting from the beginning and the end of the array. Specifically, for each index i, check if the element at the ith position is equal to the element at the (length-i-1)th position until the middle of the array is reached. If all corresponding pairs are equal, the array is a palindrome .

To determine if a string's parentheses are properly matched and ordered, you can use a stack data structure. As you iterate through the expression string, push each opening parenthesis ('{', '(', '[') onto the stack. For each closing parenthesis ('}', ')', ']'), check if the stack is not empty and if the top of the stack is the matching opening parenthesis. If so, pop the stack; otherwise, return false immediately. At the end of the iteration, if the stack is empty, the parentheses in the string are properly matched and ordered .

To determine if an array can be split into two subarrays with equal sum, first calculate the total sum of the array elements. If the total sum is not even, it is impossible to split the array as required. If the sum is even, iterate through the array, maintaining a running sum. If the running sum reaches half of the total sum at any point, the array can be split at that point, ensuring both subarrays have equal sums .

To merge two sorted arrays into one, maintain two pointers, one for each array, starting from the beginning. Compare the elements pointed to by these pointers and append the smaller element to the resulting merged array, then move the pointer forward in the array from which the element was taken. Continue this process until all elements in both arrays are exhausted. This approach runs in O(n + m) time complexity, where n and m are the lengths of the two arrays .

Matrix transposition involves swapping the row and column indices for each element, effectively turning all rows into columns and vice versa. In practical applications, transposition is used in mathematical computations, data processing for layout optimization, and input preparation for machine learning models where orientation impacts processing .

You might also like