DSA Past Interview Questions
DSA Past Interview Questions
2. What is an array?
An array is a linear data structure consisting of a collection of elements, each identified
by at least one array index or key. It is a contiguous block of memory where each
element is of the same data type, and the elements are accessed using their indices.
To access element at row index 2 and column index 3 (assuming 0-based indexing), you
would access array[2][3], which in this case is 12.
1
CSPIT, CE DSA past interview questions year 2023-24
#include <iostream>
#include <string>
int main() {
std::string str;
char target;
// Input string
std::cout << "Enter a string: ";
std::getline(std::cin, str);
// Count occurrences
int occurrences = countOccurrences(str, target);
return 0;
}
Linked List:
A linked list is a linear data structure where each element is a separate object (node)
containing both the data and a reference (link) to the next node in the sequence. The last
node points to null, indicating the end of the list. Linked lists offer dynamic memory
allocation, efficient insertion and deletion operations, and they don't have a fixed size.
2
CSPIT, CE DSA past interview questions year 2023-24
cpp
Copy code
#include <iostream>
#include <string>
int main() {
std::string input;
std::cout << "Enter a string: ";
std::cin >> input;
if (isPalindrome(input)) {
std::cout << "The string is a palindrome." << std::endl;
} else {
std::cout << "The string is not a palindrome." << std::endl;
}
return 0;
}
python
def generate_primes(n):
primes = []
for num in range(2, n + 1):
if all(num % i != 0 for i in range(2, int(num**0.5) + 1)):
primes.append(num)
3
CSPIT, CE DSA past interview questions year 2023-24
return primes
Output-based coding questions usually involve analyzing code and predicting the output
without actually running the code. These questions test your understanding of
programming concepts and language syntax.
11. Logical problem 1: A frog was at the bottom of a well that is 30 steps. It jumps 3
steps each day and falls back 2 each day. How many days will it take to reach the
top?
The frog advances 3 steps each day but retreats 2 steps, so it makes a net gain of 1 step
each day. Since the well has 30 steps and the frog makes 1 step of progress per day, it will
take 30 days for the frog to reach the top.
12. Logical problem 2: There are 100 balls in a bowl, 99 balls are of the same
weight. You are given one weighing machine and you need to find that one ball
having a different weight. How will you do that in minimum comparisons?
This problem can be solved using a binary search approach. Here's how:
13. DSA: Given a string, you need to find the occurrence of each word.
Here's a Python code to find the occurrence of each word in a given string:
python
Copy code
def count_word_occurrences(sentence):
words = sentence.split()
word_count = {}
4
CSPIT, CE DSA past interview questions year 2023-24
return word_count
14. Given an integer n and an array of size n, find the second maximum element
from the array.
Here's a Python code to find the second maximum element from an array:
python
Copy code
def find_second_max(arr):
if len(arr) < 2:
return "Array should have at least two elements."
return max2
second_max = find_second_max(arr)
print("Second maximum element in the array:", second_max)
These solutions address the logical problem, count occurrences of words in a string, and
find the second maximum element from an array.
(First related constraints. Give Brute Force Approach and then optimal approach)
5
CSPIT, CE DSA past interview questions year 2023-24
DSA Problems
1. Min and max valued character from a string:
To find the minimum and maximum valued character from a string, you would iterate
through the string and keep track of the minimum and maximum characters
encountered.
4. Merge Intervals:
Given an array of intervals, the task is to merge overlapping intervals and return a new
array of non-overlapping intervals.
7. Explain any one sort and sort the given array with that:
One common sorting algorithm is Merge Sort. It follows the divide and conquer
approach. Here's a brief explanation:
6
CSPIT, CE DSA past interview questions year 2023-24
merge_sort(L)
merge_sort(R)
i=j=k=0
7
CSPIT, CE DSA past interview questions year 2023-24
Quicksort: Generally considered one of the fastest sorting algorithms. It has an average
time complexity of O(n log n) but can degrade to O(n^2) in worst-case scenarios.
Merge Sort: Provides stable sorting with a time complexity of O(n log n) in all cases but
requires extra space for merging.
Heapsort: An in-place sorting algorithm with a time complexity of O(n log n) but slower
compared to quicksort for most datasets.
Insertion Sort: Efficient for small datasets or nearly sorted arrays with a time
complexity of O(n^2) but not suitable for large datasets.
Selection Sort: Simple and easy to implement but has a time complexity of O(n^2) and
performs poorly on large datasets.
Depending on the specific requirements and constraints of the problem, the best sorting
algorithm may vary.