Find Even Occurring Element

The question

Given an integer array, one element occurs even number of times and all others have odd occurrences. Find the element with even occurrences.

My answer

The first thing I thought of was using XOR, but then I realized that I am looking for the even number so it won’t actually work. My next idea was to use a hashtable:

1 – Start on the first elements and create a hashtable with the number of times each character is found

2 – Look for the even number in the hashtable

Read More

Find frequency in sorted array

The question

Given a sorted array that can include duplicates find the number of times that a given number appears in the array.

The solution

The first thing that came to my mind was to do a binary search for the element and then check how many times it appears to the left and how many times it appears to the right. This has the drawback that if the array has a large number of appearances for that number then the performance might be greatly degraded because we would have to linearly search left and right.

To overcome this limitation we would need to do a custom binary search that searches for the first occurrence of a value and another that searches for the last element. Once we have those indexes we can find the difference and that is the number of appearances:

Read More

Building an Android project from scratch using gradle

Since Android moved to Gradle I tought it would be a good idea to renew my post on building an Android app from scratch to use Gradle instead of ant.

The first thing we need to do is to create the folder structure for our project:

1
2
3
4
5
6
7
8
Project/
  |---build.gradle
  |---src/
       |---main/
            |---AndroidManifest.xml
            |---res
            |---java
                  |---src (Code goes here)
Read More

Publishing an Android library to Sonatype central repository

I made this little open source library for Android that I want to consume from an app I’m building. I have the easy option of checking in the .aar file into my repository as explained in my article but I want to do things right so I’m going to try to publish my app to central repository.

To get started you have to create a jira account and then create a ticket so they can create a project for you. There are a few mandatory fields:

– Summary: I am not sure what I was supposed to enter here so I just entered the name of my project. (conversion-graph)

– Group Id: The top level group you are going to use. (com.ncona)

– Project URL: I entered URL to the project on github. (https://github.com/soonick/conversion-graph)

– SCM URL: The same URL but with a .git extension (https://github.com/soonick/conversion-graph.git)

Read More

Reverse Words in a String

The question

Given an input string, reverse all the words. To clarify, input: “Interviews are awesome!” output: “awesome! are Interviews”. Consider all consecutive non-whitespace characters as individual words. If there are multiple spaces between words reduce them to a single white space. Also remove all leading and trailing whitespaces. So, the output for ” CS degree”, “CS degree”, “CS degree “, or ” CS degree ” are all the same: “degree CS”.

Read More

Consuming Android library with gradle

I have an Android project in gradle where I want to use a library packaged in an aar file. After some research I found the easiest way to consume it is by adding a flatDir repository to your build.gradle file.

1
2
3
4
5
repositories {
  flatDir {
    dirs 'libs'
  }
}

You probably already have a repositories section in your build.gradle file so you will only need to add the flatDir section. Also, make sure that you are adding it as a top level. The first time I tried I was adding it to the repositories section inside of buildscript and it was not working.

After specifying the repository you need to add your aar file inside the libs directory and reference it from the dependencies section inside build.gradle (also make sure it is top level):

1
2
3
dependencies {
  compile 'com.ncona.conversiongraph:conversion-graph:1.0@aar'
}

Now you can use your library within your app.

Read More

All permutations of a string

The question

Generate all permutations of a given string.

My solution

This is a question that I had been asked before and I wasn’t able to solve in the duration of an interview. I took some time to think about the problem and this is what I came with.

There are going to be n! permutations so that is what I should expect. The permutations of a string of length 1 is the string itself. The permutations for a string of length two is the string and the reversed string. With this in mind I came up with this recursive algorithm:

Read More

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