Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
79 views36 pages

Data Structures SDOT

The document contains questions and solutions related to data structures and algorithms topics like arrays, linked lists, stacks, trees, and backtracking. It includes questions on finding the maximum sum subarray, searching in a sorted array, subset sum problem, deleting a node from a linked list, finding the middle of a linked list, detecting loops in linked lists, stack operations, finding elements greater than elements to the right, longest valid parentheses, implementing a queue using stacks, implementing a stack with O(1) minimum operation, implementing a stack using a single queue, tree traversals, and calculating the size of a tree recursively.

Uploaded by

Chandan Mallesh
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)
79 views36 pages

Data Structures SDOT

The document contains questions and solutions related to data structures and algorithms topics like arrays, linked lists, stacks, trees, and backtracking. It includes questions on finding the maximum sum subarray, searching in a sorted array, subset sum problem, deleting a node from a linked list, finding the middle of a linked list, detecting loops in linked lists, stack operations, finding elements greater than elements to the right, longest valid parentheses, implementing a queue using stacks, implementing a stack with O(1) minimum operation, implementing a stack using a single queue, tree traversals, and calculating the size of a tree recursively.

Uploaded by

Chandan Mallesh
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/ 36

SDOT TRAINING

Question :01

Find the element that appears once in an array where every other element
appears twice

Input: arr[] = {7, 3, 5, 4, 5, 3, 4}


Output: 7
Question :02

Write an efficient program to find the sum of contiguous subarray within a one-
dimensional array of numbers which has the largest sum.
Question :03

Search Insert Position

Given a sorted array and a target value, return the index if the target is found. If not,
return the index where it would be if it were inserted in order. You may assume no
duplicates in the array.

[1,3,5,6], 5 -> 2
[1,3,5,6], 2 -> 1
[1,3,5,6], 7 -> 4
[1,3,5,6], 0 -> 0
Question :04

Given a set of non-negative integers, and a value sum, determine if there is a subset of the
given set with sum equal to given sum.

Input: set[] = {3, 34, 4, 12, 5, 2}, sum = 9


Output: True //There is a subset (4, 5) with sum 9
Solution
STEPS:

Let isSubSetSum(int set[], int n, int sum) be the function to find whether there is a subset of set[]
with sum equal to sum. n is the number of elements in set[].

The isSubsetSum problem can be divided into two subproblems


1. Include the last element, recur for n = n-1, sum = sum – set[n-1]
2. Exclude the last element, recur for n = n-1.

If any of the above the above subproblems return true, then return true
LINKED LIST
DECLARATION:

class LinkedList { public void printList() LinkedList llist = new


Node head; { LinkedList();
class Node { Node n = head;
int data; while (n != null) { llist.head = new Node(1);
Node next; Node second = new Node(2);
Node(int d) { data = d; } System.out.print(n.data + " Node third = new Node(3);
} ");
} n = n.next; llist.head.next = second;
} second.next = third;
}
Question:01

Given a singly linked list and a position, delete a linked list node at the given
position.
Input: position = 1, Linked List = 8->2->3->1->7
Output: Linked List = 8->3->1->7

Input: position = 0, Linked List = 8->2->3->1->7


Output: Linked List = 2->3->1->7
Question :02

Find the middle of a given linked list

Given a singly linked list, find middle of the linked list. For example, if
given linked list is 1->2->3->4->5 then output should be 3.

If there are even nodes, then there would be two middle nodes, we need
to print second middle element. For example, if given linked list is 1->2-
>3->4->5->6 then output should be 4.
Question:03

Given a linked list, determine whether it contains a loop in


it .
STACKS:
• Push: Adds an item in the stack. If the stack is full, then it is said to be an
Overflow condition.

• Pop: Removes an item from the stack. The items are popped in the reversed
order in which they are pushed. If the stack is empty, then it is said to be an
Underflow condition.

• Peek or Top: Returns top element of stack.

• isEmpty: Returns true if stack is empty, else false.


Question:

Find all the elements in an array that are greater than the elements to right

Example:
Input {10,4,6,5,3}
Output :5,6,10
Question:

Given a string consisting of opening and closing parenthesis, find length


of the longest valid parenthesis substring.

Input : ((()
Output : 2
Explanation : ()

Input: )()())
Output : 4
Explanation: ()()
Procedure:
Question :

Implement a queue using stack data structure


Question:

You are given N elements and your task is to Implement a Stack in which you
can get minimum element in O(1) time

1.Push operations
2.Pop operations
3. Min operations
IMPLEMENTATION
QUEUE:
Operations
Question:01

Implement a stack using single queue


BACK TRACKING

Given a string containing of ‘0’, ‘1’ and ‘?’ wildcard characters, generate all binary strings
that can be formed by replacing each wildcard character by ‘0’ or ‘1’.

Input str = "1??0?101"


Output:
10000101
10001101
10100101
10101101
11000101
11001101
TREE:
Declaration

class Node class BinaryTree BinaryTree tree = newBinaryTree();


{ {
int key; // Root of Binary Tree tree.root = new Node(1);
Node left, right; Node root;
tree.root.left = newNode(2);
public Node(int item) // Constructors
{ BinaryTree(int key) tree.root.right = newNode(3);
key = item; {
left = right = null; root = new Node(key); tree.root.left.left = new Node(4);
} }
}
BinaryTree()
{
root = null;
}
Question:

Tree Traversals

Inorder (Left, Root, Right) : 4 2 5 1 3


Preorder (Root, Left, Right) : 1 2 4 5 3
Postorder (Left, Right, Root) : 4 5 2 3 1
Question:

Level Order Tree Traversal


Question:

Write a program to Calculate Size of a tree | Recursion


THANK YOU

You might also like