# 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).

# 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.

# 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.

# 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.

# 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
``````

# 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):

# 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]]
``````

# 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>
``````

# Schedule your Android app to do something periodically

I am writing a little Android app that will send alarms to the user based on certain rules. For this there are certain things that I need:

• I want to have something that will run periodically and check my DB to see if there are any alarms I should send to the user.
• I want this to run even if my app is closed or the phone is asleep
• I want this to start automatically every time the phone is turned on

I did a little research and these are the pieces we need:

• Services – To have our app do something in the background
• AlarmManager – To schedule the service to be executed in the future
• ACTION_BOOT_COMPLETED – This is a broadcast intent that Android sends when it boots

Now, lets start putting things together.

# Setting up Google Analytics for Android

Setting up Google Analytics for a website is as simple as adding a snippet of JS on each of your pages. I was expecting the same for Android apps, but it seems like you need to follow a few steps to get this to work.

## Setting up Google Play services SDK

The Google Analytics library for Android needs to communicate with Google Play API, for this reason, we need to install the SDK. Run this command:

``````1
android
``````

And install the Google Play services package: