Sharing IDE configuration with EditorConfig

It has happened multiple times that I am doing a code review and I find issues like trailing white space or tabs instead of spaces. This annoys me because it is something that your editor should do for you for free. It annoys me even more when I ask the developer to configure their editor to remove trailing white space and they tell me they don’t know how to do it. In most scenarios they are using Eclipse or some other fancy IDE that I am not familiar with, so I can’t help them much. EditorConfig will help me with that problem.

From now on I plan to add an .editorconfig file to all my projects and simply ask my colleagues to install the plugin on their IDE.

Lets see how to install the plugin for VIM. Assuming you have pathogen installed you should only need this:

Read More

Median of Integer Stream

The question

Given a stream of unsorted integers, find the median element in sorted order at any given time. So, we will be receiving a continuous stream of numbers in some random order and we don’t know the stream length in advance. Write a function that finds the median of the already received numbers efficiently at any time. We will be asked to find the median multiple times. Just to recall, median is the middle element in an odd length sorted array, and in the even case it’s the average of the middle elements.

Read More

Search Unknown Length Array

The question

Given a sorted array of unknown length and a number to search for, return the index of the number in the array. Accessing an element out of bounds throws exception. If the number occurs multiple times, return the index of any occurrence. If it isn’t present, return -1.

My answer

Reading the question there is one question that came to my mind: Can the exception be handled?. Assuming that it can’t I think the only alternative would be to check all elements starting from the first one. I will assume the exception can be handled so I can come with a better solution.

Read More

Watching JS variables for changes

After playing with angular for a while I got curios about how it can watch for variable changes and execute a function when these occur.

I did a little research and found out that all watches that you define in Angular are evaluated by an event loop that is entered when some event is triggered or apply is called.

That didn’t sound very interesting to me, but I did find some interesting alternatives.

Read More

Kth Largest Element in Array

The question

Given an array of integers find the kth element in the sorted order (not the kth distinct element). So, if the array is [3, 1, 2, 1, 4] and k is 3 then the result is 2, because it’s the 3rd element in sorted order (but the 3rd distinct element is 3).

My solution

This doesn’t sound as a very hard problem but I couldn’t find a solution better than:

– Sort using quicksort

– Return the element at index k

The complexity of this solution is the complexity of quicksort O(nlogn).

Read More

CSS Flexbox

Today I found myself in the need to create a layout that I hadn’t done before:

This layout would be easy if I knew the widths of the elements, but I needed it to be flexible. Another possibility that crossed my mind was using percentages but there was a requirement that didn’t allow me to do it. To understand better there are a few things about the image above that I need to explain:

  • The image has a static width
  • I want the button on the right to expand and use as much space as it needs, but not more than it needs
  • I want the center portion to use all the available space left by the image and the button
Read More

Convert Array

The question

Given an array [a1, a2, …, aN, b1, b2, …, bN, c1, c2, …, cN] convert it to [a1, b1, c1, a2, b2, c2, …, aN, bN, cN] in-place using constant extra space

My solution

At first this question felt a little confusing so lets clear a few things.

– There will only be a, b and c

– The size of the array is going to be N*3

– The problem needs to be solved without creating an extra array so the modifications need to be made in the same array

Read More

Change syntastic default syntax checker

I have recently moved away from JSHint in favor of ESLint and it became annoying that syntastic uses JSHint to check my syntax. Luckily, this is easily configurable. To have syntastic use ESLint instead of JSHint I just added this to my .vimrc file:

1
let g:syntastic_javascript_checkers = ['eslint']

You can do this for any language that syntastic supports. The general format is:

1
let g:syntastic_<filetype>_checkers = ['<checker-name>']

You could even have more than one syntax checker per language if you wanted:

1
let g:syntastic_javascript_checkers = ['eslint', 'jshint']
Read More

AngularJS vocabulary

I have been playing with AngularJS for some time now and I’m still a little confused with the vocabulary they use. For that reason I decided to list the most common “things” that are part of an AngularJS app and explain what they are.

Module

A module is a container for all the other “things”(controllers, directives, filters, etc) that are part of an AngularJS app. There is usually and application level module for the whole app and smaller modules for different components. You can create a module using:

1
var someModule = angular.module('someModule', []);
Read More

Algorithm to check if a word is a palindrome

The question

Given a word, verify if it is a palindrome excluding spaces and punctuation.

The solution

This is the algorithm I came up to find out if a word is a palindrome:

– Place a pointer(left) on the first character

– Place a pointer(right) on the last character

– Check if the element at left is a character (not punctuation or space)

– If the element is not a character move the pointer one space to the right until you find a character

– Check if the element at right is a character

– If the element is not a character move the pointer one space to the left

– If left is greater than right return true

– Check if left and right elements are the same

– If they are not the same return false

– If they are the same move left one space to the right and right one space to the left

– Start over from step three

Read More