Kamal Rawat's Blog, page 5

May 28, 2018

Find numbers with more than k-bits in their binary representations

Given an array of unsigned integers, print all the numbers that have more than k bits set in their binary representation. For example, if k = 2, and input array is: int arr[] = {2, 1, 15, 9, 7, 14, 32, 127}; Then the output should be: 15 7 14 127 Because all of these […]
 •  0 comments  •  flag
Share on Twitter
Published on May 28, 2018 04:53

Remove all white spaces from a string

Given a string that may have white spaces, write code to remove all white spaces from than string. For example: Input String: "IT IS HOT OUTSIDE" Output String: "ITISHOTOUTSIDE" Input String: " I T I S HOT " Output String: "ITISHOT" Solution: The brute-force way of solving it is to shift all the elements when […]
 •  0 comments  •  flag
Share on Twitter
Published on May 28, 2018 04:26

May 19, 2018

Difference between nodes of alternate vertical levels in a tree

The question below is given in the format in which questions are asked on coding interview Platforms like HackerRank, CodeChef, CodeJam, etc. PROBLEM STATEMENT Vertical level of a node is the vertical distance of that node from the root. Vertical level of the root node is 0. If a node is at vertical level k, […]
 •  0 comments  •  flag
Share on Twitter
Published on May 19, 2018 06:17

May 6, 2018

Rearrange the nodes of a linked list

Given a linked list, rearrange the node of the list as shown below: INPUT LIST: 1 -> 2 -> 3 -> 4 -> 5 -> 6 -> 7 -> 8 OUTPUT: 1 -> 8 -> 2 -> 7 -> 3 -> 6 -> 4 -> 5 INPUT LIST: 1 -> 2 -> 3 -> 4 […]
 •  0 comments  •  flag
Share on Twitter
Published on May 06, 2018 22:53

Merge alternate nodes of the two lists

We have already seen the code to merge two sorted linked list, such that the final list is also sorted. The logic and code can be found in this post. In this post, we want to merge the two lists in a way that in the final list, element comes from the two lists alternately […]
 •  0 comments  •  flag
Share on Twitter
Published on May 06, 2018 22:46

March 8, 2018

Water overflowing from glass arranged in form of triangle

Glasses are arranged in the form of triangle (on top of each other) as shown below: 1 2 3 4 5 6 7 8 9 10 .................... ...................... Liquid is poured into 1st glass (Glass no. 1). When it is full, then extra liquid will flow into the glasses 2 and 3 in equal quantities. […]
 •  0 comments  •  flag
Share on Twitter
Published on March 08, 2018 12:08

March 7, 2018

Protected: live classes

There is no excerpt because this is a protected post.
 •  0 comments  •  flag
Share on Twitter
Published on March 07, 2018 02:11

February 11, 2018

Maximum (or minimum) sum of subarray of size k

Given an array of n integers, find the sub-array of length k with maximum sum. For example, Input Array: {6, 4, 3, 5, 1, 9, 2} k:3 Output: 15 (sum of sub-array 5, 1, 9) Solution: The brute-force solution for this problem is to find sum of all the sub-arrays of length k, and return […]
 •  0 comments  •  flag
Share on Twitter
Published on February 11, 2018 02:44

February 9, 2018

Consecutive numbers with sum equal to n

Given a positive integer n, print the consecutive numbers whose sum is equal to n. For example, n = 20 Output: 2, 3, 4, 5, 6 (because 2+3+4+5+6+7 = 20) n = 29 Output: 14, 15 Solution: To see the video solution of this problem, see the below video, else continue reading: The brute force […]
 •  0 comments  •  flag
Share on Twitter
Published on February 09, 2018 23:59

Number of ways to arrange tiles on a board

Given a board of size 2*n and tiles of dimension 1*2 each. In how many ways can we arrange the tiles so that entire board is covered. We can only place the tiles either horizontally or vertically without breaking them. For example, if the board is of length 3 (n=3), then tiles can be placed […]
 •  0 comments  •  flag
Share on Twitter
Published on February 09, 2018 07:30