Find Missing Element

The question

There is an array of non-negative integers. A second array is formed by shuffling the elements of the first array and deleting a random element. Given these two arrays, find which element is missing in the second array.

My solution

The idea that came to my mind first involved using a hash-table.

– Go over all the elements in the second array and add them to the hash table

– Go over all the elements in the first array and try to get it from the hash table. If it is not there, that’s the number.

The hash table solution has a complexity of O(n) but it uses a lot of space by creating a hash table with all the elements in the array.

Read More

Combine Two Strings

The question

We are given 3 strings: str1, str2, and str3. Str3 is said to be a shuffle of str1 and str2 if it can be formed by interleaving the characters of str1 and str2 in a way that maintains the left to right ordering of the characters from each string. For example, given str1=”abc” and str2=”def”, str3=”dabecf” is a valid shuffle since it preserves the character ordering of the two strings. So, given these 3 strings write a function that detects whether str3 is a valid shuffle of str1 and str2.

Read More

First Non Repeated Character in String

The question

Find the first non-repeated (unique) character in a given string.

My answer

At first glance I am tempted to start from the left and add each character go to a hashtable with the number of times I have seen it. Once I have gone through the whole string I will do it again now searching for the first character that returns 1.

Read More

Matrix region sum

The question

Given a matrix of integers and coordinates of a rectangular region within the matrix, find the sum of numbers falling inside the rectangle. Our program will be called multiple times with different rectangular regions from the same matrix.

My solution

Just from the question I am not completely sure what they are asking me but it sounds like I will have a matrix of integers (I will assume a 2 dimensional matrix) and then they will give me coordinates for two opposite corners and I just need to sum all numbers inside the limits of those coordinates. They also mention that the program will be called multiple times with different rectangular regions which makes me thing that I’m expected to do some kind of caching.

I spent some time thinking of how to do this in an efficient way but I couldn’t come up with something that felt right.

Read More

Check Balanced Parentheses

The question

Given a string of opening and closing parentheses, check whether it’s balanced. We have 3 types of parentheses: round brackets: (), square brackets: [], and curly brackets: {}. Assume that the string doesn’t contain any other character than these. No spaces, words or numbers. Just to remind, balanced parentheses require every opening parenthesis to be closed in the reverse order opened. For example ‘([])’ is balanced but ‘([)]‘ is not.

Read More

Anagram strings

The question

Given two strings, check if they’re anagrams or not. Two strings are anagrams if they are written using the same exact letters, ignoring space, punctuation and capitalization. Each letter should have the same count in both strings. For example, ‘Eleven plus two’ and ‘Twelve plus one’ are meaningful anagrams of each other.

My solution

The solution I came up with consists of having a hash-table where we will store how many times a character is found in string1. Then we will do the same for string2. At the end these should match.

Read More

Largest Continuous Sum

The question

Given an array of integers (positive and negative) find the largest continuous sum.

My solution

These are the steps I followed:

– Get the first number and add it to a variable where you will store the largest sum so far(largest).

– Create another variable(currentSum) where you will store the value of the current sum so far and assign the same number to it.

– Move to the next number. Calculate the sum of n1 and n2, if it is positive then assign it to currentSum.

– Check if currentSum is larger that largest. If it is, update largest.

– If currentSum became negative then assign 0 to it.

You have to go through each number in the array once, so the complexity is O(N).

Read More

Dutch national flag problem

The name of this problem comes from the Netherlands flag which consists of three colors: red, white and blue.

The question

Given an array of size n which has a random number of 1s, 2s and 3s in random order. Arrange the numbers so all the 1s are at the beginning, followed by the 2s and then the 3s.

Read More

Java Generics

Today I discovered a feature of java that I have been using for a while but I didn’t know it existed. Generics allow developers to create generic algorithms that work on collections of different data types but at the same time provide type safety.

Generics are commonly used for data structures. List is an example of a data structure that uses generics:

1
List<String> list = new ArrayList<String>();

You could have declared your list like this:

1
List list = new ArrayList();

The difference is that by using generics you make sure that you don’t accidentally try to insert something that is not a string into your list and cause a run time error. By specifying in the list declaration that this is a List of Strings you make sure that you get a compile time error if you try to add something that is not a string to the list.

Read More

Building an Android library with gradle

Android has moved away from ant and adopted gradle as its build system. I’m not very familiar with gradle but there is a feature of the new build system that makes it really appealing to migrate to it. The new gradle build system compiles libraries into an .aar (Android ARchive) which includes it’s resources and assets in a way that can be consumed by the apps that use your library.

This means you no longer need to copy the source code of the library into your project and compile both projects together, now you can just drop the .aar into your libs folder and it will work.

To use gradle with Android you need at least gradle version 1.10. I got the latest version at the time (1.12) and things worked fine for me.

Read More