Sei sulla pagina 1di 11

Arrays

1.

a. Implement an algorithm to determine if a string has all unique characters w/o using additional data structures. b. Design an algorithm and write code to remove the duplicate characters in a string without using any NOTE: One or two additional variables are fine. An extra copy of the array is not. Write the test cases for c. Extend your algorithm to remove duplicates in the string which has UNICODE characters.

additiona

this meth

2. 3.

Write a method to decide if two strings are anagrams or not. Try an O(n) algorithm for this. Write a method to implement the string length function. Also implement the same thing in O(1) Time. Input : abc Output: 3 (Hint : Think SizeOf() ) Rating: Very Easy

4.

Divide two numbers without using division and subtraction operators. Rating: Easy

3> Assume you have a method isSubstring which checks if one word is a sub-string of another. Given two strings, s1 and s2, write code to check if s2 is a rotation of s1 using only one call to isSubstring (i.e., waterbottle is a rotation of erbottlewat). 4> Write a method to replace all spaces in a string with %20. Given string: Abra ca dabra New string: Abra%20ca%20dabra 5> Write an algorithm to rotate an array by d times. For instance, if the array given is 1 2 3 4 and rotation time is 1, new array should be 4 1 2 3 6> Write a program to print a 2-D array spirally for NxN matrix. IP: N: 4 O/P: 1 2 3 4 12 13 14 5 11 16 15 6 10 9 8 7 Repeat the same for MxN matrix.. 6> Write a function to print all of the permutations of a string. 7> Reverse the words in a sentence, i.e. "My name is AbcXyz" becomes "AbcXyz is name My." Optimize for speed. Optimize for space. 8> Given two strings A and B, write a program that returns the intersection of A and B (could be null). Rating: Easy

9> You have a 99 cell array. In each cell you put an integer belonging to the set {1,2,...,100}. No two cells contain the same integer. Thus when the array filling is complete, one of the integers from 1 to 100 is not present in the array. Determine which integer is missing from the array. (The lower the running time of your algorithm, the better.) 10> Find the second smallest integer in an array of n non-sorted integers. Run-time restraint: n + O(log n) comparisons. 11> Given an array of numbers, except for one number all the others, occur twice. Give an algorithm to find that number which occurs only once in the array. i) Find the solution in O(n) time ii) Find the solution for the same in O(log n) time. Rating: Rating:

12> Given an array of 2n elements of which n elements are same and the remaining n elements are all different. Write a C program to find out the value which is present n times in the array. There is no restriction on the elements in the array. They are random (In particular they are not sequential) 13> Given two arrays A and B. Array A contains all the elements of B but one element extra. Find out the extra element. Restrictions: Dont use any relational ops ( > or > or == etc.), array elements are not in order. 14> Write a program to divide two numbers without using division operators.(Hint: Find how to multiply two numbers without using multiplication). And also write a program to add two numbers without using addition operator. 15> Given an array of length N. How will you find the minimum length contiguous sub - array of whose sum is S and whose product is P . Here S and P will be given to you. Input: array, sum and product. Output: Sub array Rating:

16> Given a stack S, write a C program to sort the stack (in ascending order). You are not allowed to make any assumptions about how the stack is implemented; the only functions allowed to be used are: Push, Pop, Top, IsEmpty, IsFull. Rating: 17> Given an array of 0s and 1s , in O(n) time and in-place,place all the 0s in one side and all the 1s in other.Don't use any additional arrays. Rating:

18> Given a n array of positive and negative integers, find the contiguous sub-array with max sum in O(n). 19> Given a n array of positive and negative integers, find the contiguous subarray(s) which has sum=0. (Note :If two continuous sub arrays have sum=0, you have print both the arrays.). Implement in O(n) Time. Eg: Input : -3,2,4,-6,-8,10,11 Output : {2,4,-6} {4,-6,-8,10} 20> Given a array A[] and a number x, find the contiguous sub-array who's sum is equal to x. A[] contains both positive and negative numbers. Find the solution in O(n) time. I/P: a[]={1, 4, -2, 8, 15} and x= 10 O/P: {4,-2,8} Rating: 1/2

21> Write a program to sort the input array. Only the following operations are allowed in the array. 1> get(index) -gets the element at that index 2> reverse(int start,int end) - example reverse(1,3) for the array [1,2,3,4,5] will return [1,4,3,2,5] Using only these functions sort the given input array. Rating: 1/2

22> Describe an algorithm for reversing the words in a string; for example My name is Abc Xyz becomes Xyz Abc is name My. An O(n) and in-place solution is appreciable. 23> You are given a sequence of numbers from 1 to n1 with one of the numbers repeating only once; e.g. 1 2 3 3 4 5. How can you find the repeating number? 2 What if given the constraint that you can use only a fixed amount of memory (i.e. independent of n)? What if there are two repeating numbers instead of one (with the same memory constraint)? 24> You are given two arrays, A and B. Array A contains all elements of the array B, and one extra element. Find the value of the extra element. Bonus: Solve the problem without using relational operators (<, >, etc). 25> Write an efficient C function that deletes characters from a string. The function shall have the prototype void RemoveChars(char str[], char remove[]); where any character existing in remove must be deleted from str. For example, given a str of "Battle of the Vowels:Hawaii" and remove of "aeiou", the function should transform str to "BttlfthVwls:Hw". 26> Write a recursive function which generates the power set of a given set. The set is passed to the function as a string, and the function should print the subsets as strings. Example: given "abc", the function should print (the empty string, encode it somehow), a, b, ab, c, ac, bc, abc. 27> a.You are given an array [a1 To an] having n numbers. You have to construct another array [b1 To bn] where bi = a1*a2*...*an/ai. For instance, b[0] = a[1]*...*a[n]/a[0], b[1] = a[0]*a[2]*....*a[n]/a[1] You are allowed to use only constant space and the time complexity is O(n). b. Variation: Try it without any divisions. 28> Given a Data Structure having first n integers and next n chars. A = i1 i2 i3 ... iN c1 c2 c3 ... cN.Write an in-place algorithm to rearrange the elements of the array such that A = i1 c1 i2 c2 ... in cn 29> Given an array of 0s and 1s (all mixed-up, i.e. theyve occupied random positions) write an algorithm to arrange all 0s to the left of the array, followed by all 1s. 30> You are given an array of integers containing only 0s and 1s.You have to place all the 0s in even position and 1s in odd position. And if suppose, no. of 0s exceed no. of 1s or vice-versa, then keep them untouched. Do that in ONE PASS and without taking extra memory (modify the array in-place). For Example :

Input Array: {0,1,1,0,1,0,1,0,1,1,1,0,0,1,0,1,1} Output Array: {0,1,0,1,0,1,0,1,0,1,0,1,0,1,1,1,1} 31> Given an array of red, green and blue balls arrange them in groups of all red together, greens together and blue together. Do in a single scan of the array 32> Given two arrays (of characters/numbers), find the a. longest common sub-sequence b. shortest common super-string Hint: Dynamic Programming 33> There are 2 sorted arrays A and B of size n each. Write an efficient (o(logn) and/or o(n))algorithm to find the median of the array obtained after merging the above 2 arrays(i.e. array of length 2n). 34> Given an array all of whose elements are positive numbers, find the maximum sum of a sub-sequence with the constraint that no 2 numbers in the sequence should be adjacent in the array. Eg. i) 3 2 7 10 should return 13 (sum of 3 and 10) ii) 3 2 5 10 7 should return 15 (sum of 3, 5 and 7) 35> a.Given an array of size n. It contains numbers in the range 1 to n. Each number is present at least once except for 1 number. Find the missing number. b.Given an array of size n. It contains numbers in the range 1 to n. Each number is present at least once except for 2 numbers. Find the missing numbers. c.Given an array of size n. It contains numbers in the range 1 to n. Find the numbers which aren't present. 36> Using recursion, reverse a given string. 37> Give an algorithm for sorting a file which has 4 billion integers. 38> Given an array arr[], find the maximum j i such that arr[j] > arr[i] and j>i. 39> i) Print all combinations of array that can compose a given sum (n). For example arr[] = { 1 ,2, 3} For n = 3, the program should print following: {1, 1 ,1}, { 1, 2 } ,{2, 1},{ 3} ii) Print all combinations of array that can compose a given sum (n) such that no repetitions possible. For example arr[] = { 1 ,2, 3} For n = 3, the program should print following: {1, 1 ,1}, { 1, 2 } , { 3} 40 > i) Given an array of size n. It contains numbers in the range 1 to n. Each number is present at least once except for 2 numbers. Find the missing numbers. Find it in O(n) time and O(1) space. Eg : Array[] = {5, 4, 5, 4, 1} Numbers 2 & 3 are missing. Find it from the array. ii) Given an array of size n. It contains numbers in the range 1 to n. Find the numbers which arent present. Find it in O(n) time and O(1) space. Eg : Array[] = {5, 5, 5, 5, 5} Numbers 1,2, 3,and 4 are missing. Find it from the array.

41 > Given an array A[], write a function to separate even and odd numbers(i.e., put all even numbers first than odd numbers) and maintain stability(see Wikipedia for Sorting Stability) of elements in the array 42> You have an array like ar[]= {1,3,2,4,5,4,2}. You need to create another array ar_low[] such that ar_low[i] = number of elements lower than or equal to ar[i] in ar[i+1:n-1]. So the output of above should be {0,2,1,2,2,1,0}. Time Complexity : O(nlogn). use of extra space allowed. Linked Lists 1> Implement an algorithm to find the nth element from last element of a singly linked list. Linked list given: 1 9 10 20 49. 2nd element from the last is: 10. Youll be given any number n and youll need to find the nth element from the last. You should not traverse the linked-list more than once (i.e. in a single pass) 2> You have two numbers represented by a linked list, where each node contains a single digit. The digits are stored in reverse order, such that the 1s digit is at the head of the list. Write a function that adds the two numbers and returns the sum as a linked list. EXAMPLE Input: (3 -> 1 -> 5) + (5 -> 9 -> 2) Output: 8 -> 0 -> 8 3> Write a program to find the linked list is circular or not. And also find the length of the loop. Implement in O(n) time complexity. Rating:

5> Given a circular linked list, implement an algorithm which returns node at the beginning of the loop. DEFINITION Circular linked list: A (corrupt) linked list in which a nodes next pointer points to an earlier node, so as to make a loop in the linked list. EXAMPLE input: A -> B -> C -> D -> E -> C [the same C as earlier] output: C Rating: 1/2

6> Write a program to remove the loop in the circular linked list. Implement in O(n) time complexity. 7) Write a program to delete the circular linked list. The circle may happen anywhere in the list. Free the linked list memory nodes. Rating: 1/2

8) Write a program to delete the nth element from last of the circular singly linked list. The loop may happen anywhere in the list. EXAMPLE Delete 4th Element from last: O/P : A->C->D->E->C Delete 2th Element from last: O/P : A->B->C->E->C Delete 3th Element from last: O/P : A->B->D->E Rating:

9> Implement an algorithm to delete a node in the middle of a single linked list, given only access to that node. EXAMPLE Input: the node c from the linked list a->b->c->d->e Result: nothing is returned, but the new linked list looks like a->b->d->e 10> Given two unsorted singly-linked lists, find the most efficient way to make a single sorted list out of them. 11> Given two sorted linked lists, l1 and l2, write a C program to compute their intersection 12> Write an algorithm to find the middle node of a single link list. You should not traverse the linked-list more than once. 13> You are given a Linked List and a function declaration as node* KReverse(node* head, int k); KReverse is a function that reverses the nodes of a Linked List k at a time and then returns the modified Linked List. For Example Linked List : 1->2->3->4->5->6->7->8->9->10->11 For k = 2 Return Value: 2->1->4->3->6->5->8->7->10->9->11 For k = 3 Return value: 3->2->1->6->5->4->9->8->7->10->11 Constraints: If the no. of nodes in a link list is not a multiple of k then left-out nodes in the end should remain as it is. You have to retain the memory address of the nodes without modifying it i.e. you can't just interchange the values in the nodes. No extra memory allowed. Implement the function KReverse. 14> Write an algorithm to insert a node into sorted linked list. After inserting, the list must be sorted. 15> Two Huge numbers are represented as two separate linked lists with each node representing a single digit. Ex: 1000 is represented as 1->0->0->0. Add the two numbers (represented by the two lists) and return the result in the same list format. For instance, if you are given two lists 2->3->4 and 1->2->3, you should return a list 3->5->7. Note: The result may have number of nodes more than the two input lists. (eg) 9->9->9 + 1 = 1->0->0->0. 16> You are given two singly linked lists, such that they start from two different nodes (head) and then, a few nodes down the list, they meet at a common node and then share all the nodes henceforth until the tail. The task is to find the first common node.

17> You are given a circular single linked list of sufficiently many number of nodes(in millions). You need to delete a node say P and you are given a pointer to P in the circular single list. Suggest the most efficient methodology of deleting

the node P from the circular single linked list without rounding about the circular single linked list. 18> Write a programme to print a linked list in reverse order in O(n) time and O(1) memory without resorting to recursion. Stacks and Queues 1> Describe how you could use a single array to implement three stacks. 2> How would you design a stack which, in addition to push and pop, also has a function min which returns the minimum element? Push, pop and min should all operate in O(1) time. 3> Implement a Queue using two stacks and vice versa. 4> Implement a program to find whether the given (arithmetic) expression is valid or not. Rating: 1/2 5> Implement a stack using a singly linked list L. The run time of PUSH and POP should be O(1). 6> Implement a queue using a singly linked list L. The run time of ENQUEUE and DEQUE should be O(1). 7> Explain how to implement two stacks in one array A[1,...,n] in such a way that neither stack overflows unless the total number of elements in both stacks together is n. The run time of PUSH and POP is O(1).

Trees and Graphs 1> Implement a function to check if a tree is balanced. For the purposes of this question, a balanced tree is defined to be a tree such that no two leaf nodes differ in distance from the root by more than one. 2> Write an algorithm to find the next node (i.e., in-order successor) of a given node in a binary search tree where each node has a link to its parent. 3> Given a directed graph, design an algorithm to find out whether there is a route between two nodes. 4> Design an algorithm and write code to find the first common ancestor(parent) of two nodes in a binary search tree. Avoid storing additional nodes in a data structure. Variation: What if the tress is not a BST but just a binary tree? 5> Given In-Order and Post-Order of a binary tree, write an algorithm to create a Binary tree from it. Eg: Post Order : D E B H G F C A In Order : D B E A G H F C Output Tree:

6> Given only Post-Order of a BST, write an algorithm to create a BST. Rating: Eg: Post Order of BST: 6 18 11 25 21 99 75 55 7> Given a binary tree, write an algorithm to verify whether it is a binary search tree? 8> Write a algorithm is to Find the Diameter of the tree..i.e level number with maximum no. of nodes. 9> How do you find out the fifth maximum element in an Binary Search Tree in efficient manner. Note: You should not use use any extra space. i.e sorting Binary Search Tree and storing the results in an array and listing out the fifth element etc. 10> Given two binary trees, write a compare function to check if they are equal or not. Being equal means that they have the same value and same structure. 11> Given a BST, write an algorithm to find its mirror. 12> Write a programme to implement level-order traversal of a tree. 13> Two binary trees T1 and T2 are isomorphic if T1 can be transformed into T2 by swapping left and right children of the nodes in T1. Give an algorithm to report whether two given binary trees are isomorphic. 14> Give an algorithm to find all the elements between 2 keys K1 and K2 with K1<=K2 in a binary search tree 15> Given a BST how will you find median in that? Constraints: No extra memory. Function should be re-entrant (No static, global variables allowed.) Median for even no of nodes will be the average of 2 middle elements and for odd no of terms will be middle element only. Algorithm should be efficient in terms of complexity. 16> Given a Binary Search Tree, write a program to print the kth smallest element without using any static/global variable. You cant pass the value k to any function also.

17> Given a binary tree find out the total number of leaf nodes. 18> Write a program to find the depth or height of a binary tree. 19> Write a program to delete a tree 20> Write a program to find the minimum and maximum value in a binary search tree. 21> Write code to implement the preorder(), inorder() and postorder() traversals. Whats their time complexities? 22> Write code to delete a node from a Binary Search Tree. 23> Write a program to find the least common ancestor of a n-ary tree. 24> Convert a min heap to BST without changing its structure and of course no extra space. Etcetera 1> Find the next number in the sequence: 1, 3, 7, 12, 18, 26, 35, 45, 56, 69, 83 2> Multiply a number by 8 without using multiplication or addition operations. Now do the same with 7. Can this be generalized for any number? 3> Write a program to convert Roman numerals to integer. Ex: I/P: XLV O/P: 45 Write also the program to convert integer to Roman numerals. 4) Write a program to generate string values for columns in MS Excel. For Example 1 means A, 27 means AA etc... 5> Write an algorithm for generating a list of all prime numbers up to a given integer N using O(N) space. Hint: Sieve of Eratosthenes And also prove the time complexity of the algorithm. 6> Write an algorithm to find n-th complement of the given number. Example: I/P : Nth Complement :2 Number : 0100 O/P : 1100 5> Two robots are to be parachuted onto random locations on an infinite line. When they land, their parachutes detach and remain where they are. The robots may be programmed from the following instruction set: Go left one unit Go right one unit Skip next instruction unless there is a parachute here Go to label (this is similar to our go-to statement in C/C++)

Each instruction takes one cycle to execute. Program the robots to collide. 6> What are the next few numbers in the sequence 1, 20, 33, 400, 505, 660, 777, 8000, 9009, 10100, 11121? hint: binary and decimal number reprsentations. 7> Suppose you are given N companies, and you are asked to merge them into one big company, how many ways are there to merge? 7> How many prime numbers exists in totality? Can you prove your conclusion? 8> How do you convert a decimal number to its hexadecimal equivalent.Give a C code to do the same. 9> Given a number n, find the nth Fibonacci number using recursion 10> Find the GCD of 2 numbers using recursion. 11> Towers of Hanoi: The towers of Hanoi problem consists of a set of three poles arranged in a row. There is a set of n disks of decreasing size stacked on one of the poles. The problem is to transfer these disks from the current pole to another by moving only one disk at a time, subject to the constraint that you cannot place a disk on top of a smaller one. Write a program to compute the sequence of moves that will accomplish the task. 12> Given a string and a number x(<=length of that string). Find all the combinations of that string with x characters. Write test cases to your programme. 13> What is a code-page. What do you know about Unicode standards? 14> You are given with three sorted arrays ( in ascending order), you are required to find a triplet ( one element from each array) such that distance is minimum. Distance is defined like this : If a[i], b[j] and c[k] are three elements then distance = max(abs(a[i]-b[j]),abs(a[i]-c[k]),abs(b[j]-c[k])) Give a solution in O(n) time complexity 15> Classic - Egg Problem You are given 2 eggs and have access to a 100-storey building. Eggs can be very hard or very fragile, meaning, it may break if dropped from the first floor or may not even break if dropped from 100th floor. Both eggs are identical. You need to figure out the highest floor of a 100-storey building an egg can be dropped without breaking, in minimum no. of drops. You are allowed to break both the eggs in the process. 16> Implement "void IntToStr(int num, char str[])" 17> Program to longest palindrome in the given string. Eg onomatopoeic tattarrattat, where tattarrattat is the longest palindrome Bit Manipulation

1> Give an algorithm to count the number of ones in a 32 bit number. 2> Give a one-line C expression to test whether a number is a power of 2. 3> Given two integers A & B. Determine how many bits required to convert A to B. Write a function int BitSwapReqd(int A, int B); 4> Reverse the bits in an integer. Eg: 2 (10) should be 1 (01) 5> Reverse the bits between j to k from least significant bit in a 32 bit integer in O(1) time. Eg. n= 11110000, j=1 and k=3 then -Arun & KK

Potrebbero piacerti anche