guilhebl

Philosophical writings about programming and technology

Fit Binary Int M into Binary Int N

Problem overview You are given two 32-bit numbers, N and M, and two bit positions, i and j. Write a method to set all bits between i and j in N equal to M (e.g.,...
Read more...
09 April 2015

angular $locationChangeStart

$locationChangeStart is a useful event listener provided by angular for intercepting route changes and whenever useful to debug or inject features while switching between paths. $locationChangeStart Sample route configuration: return app.config(['$routeProvider', function($routeProvider) { <span class="nx">$routeProvider</span><span...
Read more...
01 April 2015

Bit manipulation - Find next higher sparse number

Problem overview This is a good mental exercise and an interesting bit manipulation problem for practicing binary related logic: an integer is a sparse number if there are no adjacent 1 in it's binary representation....
Read more...
30 March 2015

Java 8 Stream API - a new way of dealing with Collections

Java 8 Stream API Perhaps one of the most used Java API is the Collections API, Lists, HashMaps, Sets, and others are used pretty much all the time, good news is that Java 8 provides...
Read more...
25 March 2015

Find max diameter of binary tree

Problem overview The max diameter of binary tree is the largest distance between 2 nodes in the tree. Example: balanced binary tree Answer: 7 unbalanced binary tree Answer: 9 Algorithm analysis The Max diameter of...
Read more...
20 March 2015

Find first K available timeslots for N people

Problem overview You are given a function: List getTimeSlots (String friend) Assume getTimeSlots() returns available times for a friend, sorted in order, with no overlap. You want to schedule a meeting among all of your...
Read more...
19 March 2015

Knapsack

Problem overview Given an array of n integers, A = { a1, a2, a3, …, an }, and another integer k representing the expected sum. Select zero or more numbers from A such that the...
Read more...
19 March 2015

Find lowest possible sequential number from numeric string while deleting k elements

Problem overview Given a numeric string S (i.e "6205123" ) of size N containing only digits and a number K where 1 <= K <= N, find the minimum number of possible from deleting K...
Read more...
19 March 2015