Spiral traversal of binary tree

binary_tree

Given a binary tree , print it’s nodes in spiral fashion. Example for Given Tree,Output should be F,B,G,I,D,A,C,E,H This is also called zig zag tree traversal. Algorithm : We usually use a queue for level order traversal of a queue. If we want to use the same here, how will we change direction of traversal Read More →

Microsoft Interview Questions – Set 3

Company : Microsoft I appeared in Microsoft interview recently and I would like to contibute to crazyforcode.com. This website is very helpful for interview preparation. Written Round: 60 minutes, 3 programs, no compiler; just type and submit. Round 1: Initially he started with a discussion of my final year project. After about 10 minutes of Read More →

Leaders in an array

Problem: You are given an array. You have to write a program that will print all the leaders in the array. An element is leader if it is greater than all the elements to its right side. And the rightmost element is always a leader. For example array {6, 7, 4, 3, 5, 2}, leaders Read More →

Eliminate the pairs (two same chars adjacent to each other) in string

Problem: You are given a string. You have to eliminate the pairs (two same chars adjacent to each other). eg. input string RGBBGBGR –> RGGBGR–> RBGR (output) Solution: We should check if we have character pair then cancel it. and then check for next character and previous character. Keep canceling the character until we either Read More →

Maximum size square sub-matrix with all 1s

Problem: Given a binary matrix consisting only 0s and 1s, find the maximum size square sub-matrix with all 1s. Example: Consider the below matrix. 0 1 1 0 1 1 1 0 1 0 0 1 1 1 0 1 1 1 1 0 1 1 1 1 1 0 0 0 0 0 The Read More →

Boolean Matrix Question

Given a matrix of size n x m filled with 0′s and 1′s e.g.: 1 1 0 1 0 0 0 0 0 0 0 1 0 0 0 1 0 1 1 0 if the matrix has 1 at (i,j), fill the column j and row i with 1′s i.e., we get: 1 1 Read More →

Replace every element with the next greatest

Given an array of integers, replace every element with the next greatest element on the right side in the array. Replace last element with 0 as there no element on the right side of it. eg. if the array is {6, 7, 4, 3, 5, 2}, output {7, 5, 5, 5, 2, 0} Method 1 Read More →

The Coin Problem – Dynamic Programming

Given a list of N coins, their values (V1, V2, … , VN), and the total sum S. Find the minimum number of coins the sum of which is S (we can use as many coins of one type as we want), or report that it’s not possible to select coins in such a way Read More →

Epic Systems interview questions – Set 1

I appeared for Epic system in DCE campus interview recently. I would like to contribute for CrazyforCode by sharing my experience. Interview process: 2 Written coding test + 1 telephonic round 11 got selected after final round Offer: 1,00,000 $ Technical Interview :- Q1. Given a long linked list find the element which is nth Read More →

Reverse every k nodes of a linked list

Given a linked list and a number k. Reverse every k nodes in the list. Example : Input 1->2->3->4->5->6 and k = 2 Output 2->1->4->3->6->5 Solution : We have a pointer q which points to the head of the list initially. Advance this k times in a while loop and keep reversing the list. Now Read More →

Rotate an array by d elements

Problem: Write a program that will rotate a given array of size n by d elements. eg: 1 2 3 4 5 6 7 and d = 3 Output : 4 5 6 7 1 2 3 Method 1: For rotating the array by d elements, first rotate it by 1. Move a[1] to a[0], Read More →

Find row with maximum number of 1s in sorted matrix

Problem: You are given a MxN matrix with each row sorted. Matrix is having only 0′s and 1′s. You have to find row wotth maximum number of 1′s. eg. matrix given: 000111 001111 011111 000011 111111 // row with max number of 1′s Method 1: Brute force approach Traverse the matrix row wise and count Read More →