0% found this document useful (0 votes)
50 views15 pages

DSA and Algo For Interview

The document discusses various array data structure concepts like traversal, insertion, deletion and dynamically changing array size. It also contains questions related to arrays, stacks, sorting algorithms, pointers and Fibonacci series. Tasks involve operations on arrays like rotation, reversing, searching, shifting elements, finding combinations and intersection/union of arrays.

Uploaded by

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

DSA and Algo For Interview

The document discusses various array data structure concepts like traversal, insertion, deletion and dynamically changing array size. It also contains questions related to arrays, stacks, sorting algorithms, pointers and Fibonacci series. Tasks involve operations on arrays like rotation, reversing, searching, shifting elements, finding combinations and intersection/union of arrays.

Uploaded by

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

Data Structures & Algorithms 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.

Q: Can we dynamically change the size of an array?


Arrays can either hold primitive values or object values. An ArrayList can only hold object
values. You must decide the size of the array when it is constructed. You can't change the size
of the array after it's constructed.
Allocate a new[] array and store it in a temporary pointer. Copy over the previous values that
you want to keep. Delete[] the old array. Change the member variables, ptr, and size to point to
the new array and hold the new size.

Link for preparation:


https://www.geeksforgeeks.org/array-data-structure/
https://www.youtube.com/watch?v=08LWytp6PNI

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.

2. Reverse given array.


[3,4,5,6,7,8,4,13,23,54,78,90]

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.

Time Complexity of Insertion Sort:


Insertion Sort is an easy-to-implement, stable sorting algorithm with a time complexity of O(n²)
in the average and worst case, and O(n) in the best case. For very small n, Insertion Sort is
faster than more efficient algorithms such as Quicksort or Merge Sort.
(c) Selection
Selection sort is a simple sorting algorithm. This sorting algorithm is an in-place comparison-
based algorithm in which the list is divided into two parts, the sorted part at the left end and the
unsorted part at the right end. Initially, the sorted part is empty and the unsorted part is the
entire list.

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).

is it good to use merge sort for an array of very small size?


Merge sort can work well on any type of data set irrespective of its size (either large or small).
The quicksort cannot work well with large datasets. Additional storage space requirement:
Merge sort is not in place because it requires additional memory space to store the auxiliary
arrays

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.

Dynamic Memory Allocation


Dynamic memory allocation is the process of assigning the memory space during the execution
time or the run time. Reasons and Advantage of allocating memory dynamically: When we do
not know how much amount of memory would be needed for the program beforehand.

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:

What’s the most efficient sorting algorithm you know?


Quicksort is one of the most efficient sorting algorithms, and this makes of it one of the most
used as well. The first thing to do is to select a pivot number, this number will separate the data,
on its left are the numbers smaller than it and the greater numbers on the right
What is the Fibonacci series?
In the case of the Fibonacci series, the next number is the sum of the previous two numbers for
example 0, 1, 1, 2, 3, 5, 8, 13, 21, etc. The first two numbers of Fibonacci series are 0 and 1.
There are two ways to write the Fibonacci series program: Fibonacci Series without recursion.

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)

Bonus Task: v imp


write a function that generates a random number between 10-and 70.

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

Bonus Task: v imp

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.

Why linked list when we have arrays.


Arrays allow random access and require less memory per element (do not need space for
pointers) while lacking efficiency for insertion/deletion operations and memory allocation. On the
contrary, linked lists are dynamic and have faster insertion/deletion time complexities
Advantages and disadvantages of linked list
Advantages Of Linked List:
● Dynamic data structure: A linked list is a dynamic arrangement so it can grow and shrink
at runtime by allocating and deallocating memory. So there is no need to give the initial
size of the linked list.
● No memory wastage: In the Linked list, efficient memory utilization can be achieved
since the size of the linked list increase or decrease at run time so there is no memory
wastage and there is no need to pre-allocate the memory.
● Implementation: Linear data structures like stack and queues are often easily
implemented using a linked list.
● Insertion and Deletion Operations: Insertion and deletion operations are quite easier in
the linked list. There is no need to shift elements after the insertion or deletion of an
element only the address present in the next pointer needs to be updated.
Disadvantages Of Linked List:
● Memory usage: More memory is required in the linked list as compared to an array.
Because in a linked list, a pointer is also required to store the address of the next
element and it requires extra memory for itself.
● Traversal: In a Linked list traversal is more time-consuming as compared to an array.
Direct access to an element is not possible in a linked list as in an array by index. For
example, for accessing a node at position n, one has to traverse all the nodes before it.
● Reverse Traversing: In a singly linked list reverse traversing is not possible, but in the
case of a doubly-linked list, it can be possible as it contains a pointer to the previously
connected nodes with each node. For performing this extra memory is required for the
back pointer hence, there is a wastage of memory.
● Random Access: Random access is not possible in a linked list due to its dynamic
memory allocation.

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

Bonus Task: v imp

just take concepts of how to detect a loop in a 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.

What is an anagram string? write a function to tell if it is an anagram string or not.

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:

Arr = [10, 11, 12]

index_arr= [1, 0, 2]

Output:

Arr = [11, 10, 12]

index_arr= [0, 1, 2]

● 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]

● Check whether an array is palindrome or not (take arrays of different sizes)


● Rearrange an array such that all negative numbers are shifted to the start indexes
while maintaining the order of positive numbers.
● Search for a given key within an array and return its index if the key is found;
otherwise, return a message indicating that the key is not present in the array.
● Identify the first unique element within an array of characters. For instance, given
the array 'abcdacda', the output should be 'b'. You can utilize count sort or a two-
pointer approach to achieve this task.
● Where to use merge sort?Is it good to use merge sort for arrays of very small
size?

● Linked-List:

● Find the largest element in the linked list.


● Find the nth element from m nodes of the linked list.
● Delete N nodes from a linked list after M nodes.
● Rotate a linked list.
● Delete a node from the middle of linked list
● Print the frequency of repeated elements in an array in O(n) complexity.
● Reverse a linked list in a single loop.
● Is it possible to access elements directly from a linked list, or is full traversal
necessary?
● Time complexities of bubble ,insertion, selection,merge and quicksort? Just time
complexities.

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?

You might also like