Problem Given an array of positive integers. Find the first and second largest elements in the array. You are not allowed to use more than one loop in your solution. Solution The solution is to use a single loop to find the first and second maximum. The current array element is compared to both first
Find two largest elements array Given an array of positive integers. Find the first and second largest elements in the array. Largest two numbers in an array solution One solution is to use two loops. The first loop finds the first maximum and the second loop finds the second maximum. Note that the second loop
Greedy algorithm interval scheduling problem An event starts at 9AM and finishes at 6PM. Several volunteers have signed to the event each providing a time period during which they can help. We need to cover the entire time of the event (9AM-6PM) with the least number of volunteers. Assume the next volunteer in line can
Optimized version of bubble sort Bubble sort is a slow comparison based sorting algorithm. Can you suggest one way to optimize it. Optimized bubble sort complexity and solution Bubble sort works by repeatedly visiting all elements of the list to be sorted comparing each pair of adjacent elements and swapping them if they are not
Most repeated character string Given a string of characters find the character that is repeated the most. Solution This is almost the same as the First None Repeating Char problem. We are going to borrow the same code however we will slightly modify it. The only code that we need to add is calculating the
Integer division problem definition Given the following function which computes integer division. Debug the code and identify the major bugs. Modify the code to fix those bugs. Assume all integer variables are within range and no overflow is about to happen. Integer division explained (1) If the numerator is equal to the denominator the function
Problem Write a function to compute the sum of integers between two given integer values. Solution The naive solution is to loop by going from the lower integer ending at the higher integer while calculating the sum. The other solution is compute the sum using the following formula Sum (1..n) = n (n + 1)/2
Problem Develop an algorithm to print the first none repeating character in a string for example the first none repeating character in the string “baby” is “a” Hash character in string This is a typical interview question. A better solution to solve this problem in linear time is to use a hash table on the
Find non repeating character string Develop an algorithm to print the first none repeating character in a string for example the first none repeating character in the string “baby” is “a” Algorithm A brute force method requires us to use two nested loops for character comparison. The moment we find a match we break the
Problem Write a C++ function that takes a character as input and returns true if the second significant bit of the binary representation of the character is set (i.e equals to 1) otherwise return false. Solution This is a bitwise operations problem. First you need to shift the character one digit to the right then