You are on page 1of 1

# 1.Given an array where the first and 2nd haalf of the array are sorted.

Print the sorted array. -- O(n) ...Space complexity is O(n) Ans: Take two pointers- 1st pointing to 1st half of sorted array and 2nd to 2nd half of sorted array. 2. Remove the duplicates in a linked list. O(n2) space complexity is O(1). Ans: for each node we are checking data of all subsequent nodes (see two while loops) hence
complexity is O(n^2).

3. Given an array of n integers, Display the numbers with even frequency. O(n2) Check for better complexity using hash table. 4. Implement a stack using two queues (no coding necessary) For O(1) push() and O(N) pop(): For O(N) push() and O(1) pop(): 5. Check if a tree is a binary search tree.

Inorder comes in ascending order. 6. Reverse a string. Take two pointers. 1St points to 1st index and 2nd point to the last index. {Swap} O(n). 7. Given two sorted arrays, merge them into result array with sorting. Time and Space
Complexity. O(n1+n2)both spcae and time 8. Given a ternary string, you have to count the total number of contiguous substrings (contigious set of characters), that you can form from this given string such that they comprise of either only one or two different characters.

9.For an array of n integers and a number k between 2 and n, give an algorithm to
determine if there are k elements that sum to zero. What are the time and space complexity? Recursive : return (func (n-1),(k-1),sum-a[n-1] || funct (n-1),k,sum) 10. The Celebrity Problem 1. Graph & Adjacency matrix (O(n2)). 2. Stack (O(n)).