Sunday, August 16, 2020

A few Videos on Data Structures and Algorithms

 

Some Programming Questions

 Some Programming Questions


I was encouraged to write this blog by my friend Vinay Dabholkar. I thank him very much. I hope you find it useful.

I have “served” in the Indian IT Industry for about 14 years. I have conducted entry-level interviews. I have also been a faculty member at a private University at Bengaluru for a semester.

The most important skill I look for is “coding”, i.e., the ability to write a computer program.

One of my most favourite questions is write a method to find the minimum in an array. Many students will write the “main” method with print statements. Many will hard code an array. Many will make the mistake of sorting the array. If you got the method right, I will ask if the array can be empty and have you taken care of it.

Apart from coding, other questions include RDBMS and SQL. Nowadays students are expected to know a lot of current trending topics such as Data Structures and Algorithms, Data Science, Machine Learning and IoT.

I am listing below a few questions/problems. Most of them can be answered using a language like Java or C++. Handle all kinds of situations that can occur. Write the most efficient code,


  1. Write a method which finds the sum of an integer array.

  2. Write a method which finds the average of an integer array.

  3. Consider the statement: double d = 1/2;

What is the value of d?

  1. Consider the method:

int m(int x) {

x = x / 2;

}

// main program

m(y);

What is the value of y after m is called?

  1. Consider the method:

int m(int x) {

x = x / 2;

x++;

x++;

x++;

x = x++;

return --x;

}

// main program

int y = m(7);

What is the value of y after m is called?

  1. Write an efficient method which checks whether a given array contains a certain element. How will you do this using templates in C++ or generics in Java?

  2. Write an efficient method which counts the occurrences in a given array of a certain element. How will you do this using templates in C++ or generics in Java?

  3. Is it always possible to do binary search on an array? Write code for binary search.

  4. Write a method that merges two sorted arrays into a single sorted array.

  5. What is a stable sorting algorithm? Give examples of stable and unstable sorting algorithms.

  6. What is the most efficient way of finding the middle-valued element of an array of numbers?

  7. How can I find the minimum and maximum values in an array of numbers using the least number of comparisons?

  8. What is the best way to represent a set? What are the times for set operations?

  9. Write a class that implements a queue without using arrays or ready-made collections or STL classes.

  10. Write a class that implements a stack without using arrays or ready-made collections or STL classes.

  11. What is an abstract data type?

  12. Write an interface for stack.

  13. Write an interface for queue.

  14. What are the ways of representing a directed graph?

  15. Write a method to check if a directed graph has a cycle.

  16. Write a method to check if a node can be reached from another in a directed graph.

  17. Write a method to find the 3rd smallest element in an array.

  18. Write a method to find the kth smallest element in an array.

  19. Describe a data structure for storing words of a dictionary. It should be possible to search for any word in O(n) time where n is the word’s length. Write the search method,

  20. What is a hash table? What is the difference between separate chaining and probing?

  21. Write methods for insert and delete for a queue using a circular linked list. How is a circular linked list better than a simple linked list?

  22. List applications of stacks.

  23. List applications of queues.

  24. List applications of heaps.

  25. What are AVL trees? What are their advantages?

  26. Write a method to reverse an array.

  27. Write a method to find a given element in a sorted array efficiently.

  28. Write a method that takes in two linked lists and joins them efficiently.

  29. Write a method that will sort an array of size n in ascending order using O(1) extra space with a guarantee of O(n log n) worst case time.

  30. Two linked lists of sizes m and n are the same beyond a common node. Write the most efficient code for finding the first common node. What is the time complexity?

  31. Write a method to compute factorial of n which is efficient in time and space.

  32. Write a class for a binary search tree with insert, delete and search methods.

  33. Write a class for a general tree with insert, delete and search methods.

  34. Write a method for in-order traversal of a binary tree.

  35. Write a method which reads a string containing digits and operators +, -, *, / and evaluates it.

  36. There is an array with positive and negative numbers.

Write a method to find a contiguous part of the array with the maximum sum.

  1. Write an efficient method returning the Fibonacci of n.

  2. Write an efficient method to multiply two matrices.

  3. Write a method to find the least costs of travelling by air from Mumbai to all cities in India assuming fixed fares.

  4. Determine the Merge Point of 2 Linked Lists

  5. Find distinct elements common to all rows in a Matrix

  6. Determine the Least Common Ancestor in a Binary Search Tree

  7. Print the maximum square sub matrix of given size

  8. Maximum size rectangle binary sub-matrix with all 1s

  9. Palindrome Partitioning

  10. Determine common elements in all rows of a given Matrix

  11. Return the kth smallest element from an array of integers

  12. Construct the Binary Search Tree given the Pre-order traversal

  13. Merge 2 sorted arrays

  14. Merge 2 sorted linked list such that the merged list is in descending order

  15. Given a Circular Linked list find the element at 1st position after n shifts.

  16. Given an array of numbers sorted in ascending order and a key, find the value in the array closest to the key.

  17. Given a string of characters, find the length of the longest substring which is a palindrome.

  18. Given a bag of limited weight capacity and a number of items each with a cost and weight, find how the bag can be filled to maximize the cost of the bag.

    1. Case 1: Assume that an item can be picked up even partially,

    2. Case 2” Assume that an item is either picked up or not picked up.

  19. Given n-letter sequences of characters, we say a sequence is directly related to another if the former can be converted to the latter by changing only one character. Given a set of n-letter sequences, find if a sequence s can be connected to another d either directly or indirectly.

  20. Given a set of points in a plane, find the least distance between any two points.

  21. Assume there are n people who are on a road at different positions. There are n houses aat different locations along the road. How will you assign the houses the people so that the overall time taken for them to reach their houses is minimized?

  22. Given two strings, find the length of the longest common subsequence.

  23. Given a linked list, write a method to reverse it without creating any new node.

  24. How will you cut a rope of length n into smaller parts of integral length so that the product of the lengths is maximized?

  25. Given binary trees t and s, check if s is a subtree of t.

  26. Given a piece of gold weighing m grams, break it into smaller pieces each of integral number of grams so that the net price is maximized. You have different prices for different weights.

  27. Given a linked list, delete all occurrences with a given kay.

  28. Given a binary tree containing values, check if it is a binary search tree.

  29. Given a binary tree and a key, find the lowest level where the key occurs.

  30. Given a sequence of cells each with a number of 1 Rupee coins, find the maximum money that can be had if I am allowed to pick up coins from a cell only if I am not picking up from a cell to its right and from the 2 immediate cells to its right.

  31. Given a graph with units on the x axis starting from 0 and going till n, and different height on the y axis, find the maximum area rectangle that can fit in it.

  32. Given a matrix, find the maximum sum subarray.

  33. Given a matrix, find the maximum area of a rectangle with all zeros.

  34. Trial Cars Problem.

  35. Four Queens Problem: Given a 4 x 4 chess board, arrange 4 queens on it so that all the queens are safe from each other.

  36. What is the best way to multiply two n-degree polynomials?

  37. Activity Selection: Given n activities with positive integral start and end times, find the maximum number of activities that can be done if only one activity can be done at a time.

  38. Job Sequencing: Given n jobs each with a positive integer deadline and a profit that can be gained if it is completed before the deadline, find the maximum profit, if only one job can be done at a time.

  39. Shelves Fitting.

  40. Egyptian Fraction: Given any fraction less than 1 in the form n/d, express it as a sum of fractions of the form 1/b where each b is unique. For example, 2/3 = ½ + 1/6.

  41. Maximum Subarray Product: Given an array of integers (positive and negative), find the maximum product of any subarray.

  42. Minimum Subarray Product: Given an array of integers (positive and negative), find the minimum product of any subarray.

  43. Given an undirected graph and an integer m > 0, write a method to check if the vertices can be colored using m different colors so that no two adjacent vertices are of the same color.

  44. Implement a stack using queues.

  45. Implement a queue using stacks.

  46. Search for a key in a sorted array.

  47. Implement a stack with an additional operation which gets the minimum value in the stack efficiently with/without using an auxiliary stack.

  48. Find the third largest element in an array.

  49. Find the fourth largest element in a linked list.

  50. Check if a binary tree is full, perfect or complete.

  51. Check if a string of opening and closing parentheses is balanced.

  52. How many reversals of “(“ and “)” are needed to make a string of parenthesis balanced?

  53. Reverse a string.

  54. Reverse every word in a string.

  55. Word fitting with maximum profit.

  56. Word processing with minimum cost.

  57. Determine if a directed graph has cycles.

  58. Topological sort in directed graph.

  59. Evaluate a postfix expression.

  60. Evaluate an infix expression containing integers +, -, *, ., { and ).

  61. Convert from infix to postfix.

  62. Sort a linked list in O(1) space.

  63. Interleave 2 linked lists in O(1) space.

  64. Delete all occurrences of a value from a liked list.

  65. Delete all occurrences of a value from a doubly liked list.

  66. Check if a linked list has a value.

  67. Get least level of a key in a binary search tree.

  68. Get n th number in sequence of multiples of 5 and 7.

  69. Get LCM of two integers.