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

Using ESLint to enforce JS coding conventions

I spend a decent amount of time reviewing code at work. After a while I get tired of reminding developers that the opening brace for an if statement goes in the same line. JSLint and JSHint do a good job of preventing developers from doing things that would break your code but it doesn’t really allow you to enforce coding conventions the way I wanted.

I was looking for a way to create custom rules for JSHint so I could enforce our team conventions but after some research I found that it is not possible and it is not something we should expect from the project. It seems like Nicholas Zakas wanted the same thing as me and he didn’t find it, so he built it. That is how ESLint was born. It is a relatively new project but I gave it a try and it seems to work very well.

You can use npm to install it globally:

1
npm i -g eslint
Read More

Creating a library for Android

I already went through the process of creating an application from the command line. This time I am going to show how to create a library project and integrate it into your app.

To create your library project create a folder and execute this command from inside that folder:

1
2
3
4
android create lib-project \
--name <project_name> \
--target 1 --path . \
--package com.example.whatever

Then you can you ahead and write some code. I created a view that will print a hello message (src/com/ncona/hello/Hello.java):

Read More

Array Pair Sum

This is the first post on a series where I will be resolving some common coding interview questions.

The question

Given an integer array, output all pairs that sum up to a specific value k.

My solution

I like to start by creating a test for my solution:

1
2
3
var arr = [1, 4, 2, 6, 8, 3, 9, 0, 7];
var res = findPairs(arr, 7);
// I expect res to be [[1, 6], [4, 3], [0, 7]]
Read More

Building back stack correctly when coming from a notification on Android

After I added some notifications to my app I noticed that they weren’t behaving the way I wanted them to behave. When I clicked on a notification I landed on one of my internal screens. When I clicked back I was expecting to go to the main screen but instead it went back to the phone home.

After reading a little I discovered that there is a difference between navigating back and navigating up (Read the related links to learn more), and that I needed to properly set the up navigation before I could fix this.

To set the up navigation correctly you need to specify a parent for each of your activities in your manifest:

1
2
3
4
5
6
7
<activity android:name="Internal" android:label="@string/app_name"
       android:screenOrientation="portrait"
       android:parentActivityName=".MainActivity">
    <meta-data
           android:name="android.support.PARENT_ACTIVITY"
           android:value=".MainActivity" />
</activity>
Read More