Largest Rectangle – Part 1

histogramOn and off, during the past couple days I spent time soling the Largest Rectangle challenged form HackerRank ( The challenge is described as follows:

“There are N buildings in a certain two-dimensional landscape. Each building has a height given by h in [1 : N]. If you join K adjacent buildings, they will form a solid rectangle of area K * min(h, … , h). Given N buildings, find the greatest such solid area formed by consecutive buildings”. Continue reading “Largest Rectangle – Part 1”

Why Apache Cassandra?

cassandra_logoThe following question came up a couple weeks ago: Why would I use Apache Cassandra? First of all let’s get a definition for Apache Cassandra. Edited from Wikipedia (

“Apache Cassandra is a free and open-source distributed database management system designed to handle large amounts of data across many commodity servers, providing high availability with no single point of failure. Cassandra offers robust support for clusters spanning multiple datacenters, with asynchronous master less replication allowing low latency operations for all clients. Continue reading “Why Apache Cassandra?”

Experimenting with Git

git_logoWhat is Git? Git is a source code control repository. It is useful if the need comes to revert to a previous version of the code. One can use it to find differences between versions. It supports branches, so one can try new features or changes and be able to get back to the working version at any point in time.

Git is distributed. Every developer has their own copy of the source code. This allows for fast access when needed allowing developers to work offline. That said; Git allows developers to merge their versions and share the updates with other members in the group. Continue reading “Experimenting with Git”

Serialization : Should I use it?

serialization-deserialization-in-java-object-streamsWhat is serialization? To answer this question I took a look at Wikipedia and edited their definition.

Serialization is the process of translating data structures or object state into a format that can be stored (e.g., in a file or transmitted across a network connection) and reconstructed later in the same or another computer environment. For many complex objects, such as those that make extensive use of references, this process is not straightforward (e.g., an object from a class that has fields made up of combinations of other classes. Serialization of object-oriented objects does not include any of their associated methods with which they were previously inextricably linked. For that purpose, other mechanisms (e.g., JSON, XML) are available. The opposite operation, extracting a data structure from a series of bytes, is named deserialization. Continue reading “Serialization : Should I use it?”

Strings – Making Anagrams

anagram_sampleIn this blog entry I generated a solution for the Strings: Making Anagrams challenge at HackerRank.

For a description of the challenge please refer to the HackerRank web site using Strings: Making Anagrams.

I received a comment regarding two items:

Item Comment
1 I can’t see the utility of the line char c = s.charAt(i);
2 And the line hist += 1; has incompatibility error.

It has been almost a year since I posted this entry. Had to look through several workspaces to locate the actual source code and updates the project. Today I am using Eclipse IDE Version: Oxygen.1a Release (4.7.1a). Apparently the projects in that workspace were not compatible. Eclipse did whatever it needed to do and the source code appeared on the screen. Ran the code and all seems to be working fine.

Item 1

The only reason for it is testing. I like to see what the code is doing. I write several hundred thousand lines of code a year using different programming languages and IDEs. I like to use simple and easy to follow code. Typically with enough comments that remind me what and why I wrote code segments (which may be single lines).

The line in question could have been condensed as follows:

			// **** get the current character from the string ****
			char c = s.charAt(i);
			// **** increment the frequency associated with this character in the histogram ****
			hist += 1;
			// **** alternately condensing the above two statements into one ****
//			hist[s.charAt(i) - 'a'] += 1;

Item 2

Not sure why that statement is producing such error. The actual statement is:

hist += 1;	// NOT:  hist+= 1;

Lately (in the past year or so), I am using a different mechanism to surround code which produces nice output. The previous mechanism was not that great. Sorry is that caused confusions.

Following is a console screen capture of the Eclipse IDE when I tried the sample data:

<<< hist: c(1) d(1) e(1) 
<<< hist: a(1) b(1) c(1) 
numberNeeded <<< deleted: 4

The proper answer is 4. The reason for this is that ‘d’, ‘e’, ‘a’ and ‘b’ characters are not in both strings.

The approach that I used is to generate two histograms with the character counts in each string. The counts in the arrays are in alphabetic order where ‘a’ matches 0, ‘b’ matches 1 and so forth and so on. The final logic is to determine how many characters are in one array but not in the other.

The actual Java code for my solution follows:

package john.canessa.anagrams;

import java.util.Scanner;

public class Solution {

	final private static int totalLetters 	= 26;	// [a - z]

	 * print histogram
	private static void print(int[] hist) {
		System.out.print("<<< hist: ");
		for (int i = 0; i < totalLetters; i++) {
			if (hist[i] != 0) {
				System.out.print((char)(i + 'a') + "(" + hist[i] + ") ");
	 * array with histogram of letters
	private static int[] histogram(String s) {
		int[] hist = new int[totalLetters];
		// **** traverse the string from start to finish ****
		for (int i = 0; i < s.length(); i++) {
			// **** get the current character from the string ****
			char c = s.charAt(i);
			// **** increment the frequency associated with this character in the histogram ****
			hist += 1;
			// **** alternately condensing the above two statements into one ****
//			hist[s.charAt(i) - 'a'] += 1;
		return hist;
	 * count characters needed to be deleted from both histograms
	private static int countCharacters(int[] firstHist, int[] secondHist) {
		int deleted	= 0;
		for (int i = 0; i < totalLetters; i++) {
			if (firstHist[i] != secondHist[i]) {
				deleted += Math.abs(firstHist[i] - secondHist[i]);
		return deleted;
	 * count number of deleted characters
	public static int numberNeeded(String first, String second) {
		int	deleted	= 0;
		// **** build histogram with letters from first string ****
		int[] firstHist = histogram(first);
		// **** build histogram with letters from second string ****
		int[] secondHist = histogram(second);

		// **** count different characters on histograms ****
		deleted = countCharacters(firstHist, secondHist);
		System.out.println("numberNeeded <<< deleted: " + deleted);

		// **** return number of deleted characters ****
		return deleted;

	 * test code
	public static void main(String[] args) {
		Scanner in 	= new Scanner(;
		String a 	=;
		String b 	=;
		System.out.println(numberNeeded(a, b));

The print() method is used for testing purposes only. References to that method are commented out in the final solution.

If you have comments or questions on this or any other blog entry, please send me a message via email.


Follow me on Twitter:  @john_canessa

Arrays – Rotate Left

rotate_arrayThis is the first of a set of challenges (I believe the series / set is relatively new) in the HackerRank web site. You may find them at HackerRank under All Domains -> Tutorials -> Cracking the Coding Interview.

The challenge is to develop the body for a method / function that rotates left an array of n integers k times. Continue reading “Arrays – Rotate Left”

Shell Sort

shell_sortI spent some time reading (section 2.1 in the Algorithms fourth edition by Robert Sedgewick and Kevin Wayne) and experimenting with the Shell Sort algorithm.

The Shell sort algorithm is an optimized Insertion sort. The idea is to reduce the number of exchanges by presorting elements in sub sequences. Continue reading “Shell Sort”

I-Node Ext2 Linux File System

minix_mascotA few weeks ago I was talking with a colleague about Linux file systems. There are several file systems supported by the Linux operating system some of which have been created for it (e.g., Ext2, Ext3, Ext4) and other by different vendors (e.g., FAT, FAT32, HFS, MINIX, NTFS, System V, BSD).

Every time I run into MINIX it brings up good old memories. It was used at school to teach operating systems. I purchase a book that came with a copy of the OS. It could boot in a regular PC. Continue reading “I-Node Ext2 Linux File System”

Reverse Fibonacci Series

fibonacci_sequenceEarlier today I was looking at a challenge. The problem was stated as:

“Print in reverse order a Fibonacci series”.

I have learned and used a couple times the Fibonacci series but time goes by and I am a firm believer that one should not memorize what you can look up. Software developers would look up the definition or better yet, a Class and associated method that would generate it.

Let’s start with a definition of what a Fibonacci series is from Wikipedia: Continue reading “Reverse Fibonacci Series”