DSA and Algo For Interview
DSA and Algo For Interview
Questions
Task 1
Arrays:
Q: What is an array?
In computer science, an array data structure, or simply an array, is a data structure consisting of
a collection of elements, each identified by at least one array index or key. An array is stored
such that the position of each element can be computed from its index tuple by a mathematical
formula.
Array traversal
To traverse an array means to access each element (item) stored in the array so that the
data can be checked or used as part of a process. In most high-level languages, it is
necessary to create a variable that will track the position of the element currently being
accessed.
Insertion
Insert operation is to insert one or more data elements into an array. Based on the
requirement, a new element can be added at the beginning, end, or any given index of
the array. Here, we see a practical implementation of the insertion operation, where we
add data at the end of the array.
Deletion
Deletion refers to removing an existing element from the array and re-organizing all
elements of an array.
Make sure that you do all the above operations. Pseudo and on the computer along with the
other basic array operations.
Task 2
1(a). Rotate an array of sizes 5 and 10.
1(b). Left rotate an array by k=5 times.
1(c). Rotate only even indexes of an array.
1(d). Rotate prime nos of an array.
Task 3
Stack
A stack is a linear data structure that follows the principle of Last In First Out (LIFO). This
means the last element inserted inside the stack is removed first. You can think of the stack
data structure as a pile of plates on top of one another.
Query
A query can either be a request for data results from your database or for action on the data, or
both. A query can give you an answer to a simple question, perform calculations, combine data
from different tables, add, change, or delete data from a database.
1. sorting
(a) Bubble
Bubble sort, sometimes referred to as sinking sort, is a simple sorting algorithm that repeatedly
steps through the list, compares adjacent elements, and swaps them if they are in the wrong
order. The pass of the list is repeated until the list is sorted.
Time Complexity of Bubble Sort:
Bubble Sort is an easy-to-implement, stable sorting algorithm with time complexity of O(n²) in
the average and worst cases – and O(n²) in the best case.
(b) Insertion
Insertion sort is a simple sorting algorithm that builds the final sorted array one item at a time. It
is much less efficient on large lists than more advanced algorithms such as quicksort, heapsort,
or merge sort.
Insertion sort is a simple sorting algorithm that works similar to the way you sort playing cards in
your hands. The array is virtually split into a sorted and an unsorted part. Values from the
unsorted part are picked and placed at the correct position in the sorted part. Algorithm To sort
an array of size n in ascending order:
1: Iterate from arr[1] to arr[n] over the array.
2: Compare the current element (key) to its predecessor.
3: If the key element is smaller than its predecessor, compare it to the elements before. Move
the greater elements one position up to make space for the swapped element.
The smallest element is selected from the unsorted array and swapped with the leftmost
element, and that element becomes a part of the sorted array. This process continues moving
unsorted array boundaries by one element to the right. This algorithm is not suitable for large
data sets as its average and worst-case complexities are of Ο(n2), where n is the number of
items.
Time Complexity of Selection Sort
O(n2) In computer science, selection sort is an in-place comparison sorting algorithm. It has an
O(n2) time complexity, which makes it inefficient on large lists, and generally performs worse
than the similar insertion sort.
Task 4
1. sorting
(a) Merge Sort (Don’t write codes by yourself just dry run the already
written codes and get the logic and concepts )
Merge sort is a sorting technique based on the divide and conquer technique. With worst-case
time complexity being Ο(n log n), it is one of the most respected algorithms. Merge sort first
divides the array into equal halves and then combines them in a sorted manner.
Time Complexity:
Merge Sort is a stable sort which means that the same element in an array maintain their
original positions with respect to each other. The overall time complexity of the Merge sort is
O(nLogn). It is more efficient than it is in the worst case also the runtime is O(nlogn) The space
complexity of Merge sort is O(n).
Question:
Where to use merge sort?
Mergesort is used when we want a guaranteed running time of O ( n log n ) O(n \log n) O(N log
N), regardless of the state of the input. Mergesort is a stable sort with space complexity of O
( n ) O(n) O(n).
Task 5
As u know some basics of the array and two-pointer approaches let's solve some problems.
1. Check whether an array is a palindrome or not (take arrays of different sizes)
2(a)Shift all the zeros at the end of an array.
[0,3,2,0,0,9,12,4,0] → expected output is [3,2,9,12,4,0,0,0,0]
2(b)Shift all negative nos at the start indexes of an array (same as above but a lil change in
condition)
3(a)Search whether a given key is in an array or not and return the index if the key is found.
(take array and key by yourself)
Bonus Task: v imp
3(b) find the first unique element in an array. Let's say we have ‘abcdacda’
the output should be ‘b’ (you can use count sort and two-pointer approach as well)
Question:
What are pointers, dangling pointers, memory leak, wild pointers, null pointers, and dynamic
memory allocation?
Pointer:
In computer science, a pointer is an object in many programming languages that stores a
memory address. This can be that of another value located in computer memory, or in some
cases, that of memory-mapped computer hardware.
Dangling Pointer:
Dangling pointers and wild pointers in computer programming are pointers that do not point to a
valid object of the appropriate type.
Memory Leak:
In computer science, a memory leak is a type of resource leak that occurs when a computer
program incorrectly manages memory allocations in a way that memory which is no longer
needed is not released. A memory leak may also happen when an object is stored in memory
but cannot be accessed by the running code.
Wild Pointers:
Uninitialized pointers are known as wild pointers because they point to some arbitrary memory
location and may cause a program to crash or behave badly. /* Some unknown memory
location is being corrupted. Please note that if a pointer p points to a known variable then it's not
a wild pointer.
Null pointers
In computing, a null pointer or null reference is a value saved for indicating that the pointer or
reference does not refer to a valid object.
Task 6
1. Find maximum and minimum nos in an array using minimum comparisons.
2. Find all combinations in an array whose sum = a given number.
3. Find the intersection and union of two arrays.
Bonus Task: v imp
Swap 2 integers a and b without using another variable.
Find a number that is even or odd without using any “if“condition.
Question:
Task 7
1. Complete all the previous tasks related to arrays.
1(a). Explore more arrays-related important programs.
2. heap sort(just dry run the pseudo-codes)
Question:
Write a function to get factorial.
Task 8
1. Linked List Basic.
Traversal of linked list
Insert at the last of the linked list
delete the first node
Find the middle node of the linked list. To solve you can use whatever approach you want but
keep in mind the slow and fast pointer approach.
Question:
What is the head pointer, what is the last pointer?
The data and address part of the node you should familiar with it.
Task 9
As basics have been done now let's jump into some programs
1. Rotate a linked list
2. Delete a node from the middle of the linked list
Question:
can we access elements directly from a linked list? or full traversal is necessary?
Task 10
1. Reverse a linked list
2. Find the intersection node of two linked lists
Bonus Task: v imp
No
Question:
how we will return the address of the previous node in a linked list?
Task 11
1. Find the nth node from the end of the linked list.
2. Check whether a given linked list is palindrome or not.
2(b).add two linked lists.
Bonus Task: v imp
if 100 passed to an array it should return 101 and if 101 passed it should return 100 conditions:
you
cannot use the if-else.
Question:
Pointers are passed by ref or by value?
What is a factory design pattern?
Also, take concepts and basic operations of double and circular linked lists.
Task 12
1. Insert Delete and search in a double and circular linked list
2. Add a node at the front of the double-linked list.
Bonus Task: v imp
check whether a linked list is a circular linked list or not.
Question:
MVC architecture and its advantages.
What is a copy constructor and the cases in which it is called?
Overhead of merge sort.
How the server processes a request generated from the browser.
Task 13
Queues
Practical Tasks:
● Array traversal
● Insertion
● Deletion
● Can we dynamically change the size of an array?
● Find missing element in array
● Reversing the string
● Merge two unsorted arrays and arrange in descending order
● Given two arrays, arr and index_arr, both of the same size, write a function
reorder_array(arr, index_arr) that reorders the elements of arr according to the
indices provided in index_arr. You cannot use the length or size function of arrays
to determine the size of the arrays.
Example:
index_arr= [1, 0, 2]
Output:
index_arr= [0, 1, 2]
● Linked-List:
Analytical Questions
● Given a 5-liter jug and a 3-liter jug, the objective is to measure either 4 liters or 7
liters of water using these jugs.
● Four people with different crossing times (1 minute, 2 minutes, 5 minutes, and 8
minutes) need to cross a bridge within 15 minutes, using a torch that lasts 15
minutes. They can only cross in pairs and cannot cross without the torch. How
can they all safely cross the bridge within the given time frame?
●