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

Binary Search Tree Check

The question

Given a binary tree, check whether it’s a binary search tree or not.

My solution

I cheated a little in this question because I didn’t remember what a binary search tree is. After checking wikipedia I found the characteristic of a BST:

– The left subtree of a node contains only nodes with keys less than the node’s key.

– The right subtree of a node contains only nodes with keys greater than the node’s key.

– The left and right subtree each must also be a binary search tree.

– Each node can have up to two successor nodes.

– There must be no duplicate nodes.

Read More

Transform word

The question

Given a source word, target word and an English dictionary, transform the source word to target by changing/adding/removing 1 character at a time, while all intermediate words being valid English words. Return the transformation chain which has the smallest number of intermediate words.

The solution

There are two parts to solving this problem. First we need to create a graph of the dictionary where each edge corresponds to a valid transformation of a word. We can represent this graph using a hash table (an object in JS). Then we do a breadth first search on this graph and that will give us the most efficient path.

Read More

Linked List Remove Nodes

The question

Given a linked list of integers and an integer value, delete every node of the linked list containing that value.

The solution

The tricky part about this problem is taking care of the edge cases. You have to go through all the elements in the list, so the efficiency is O(n).

  • Initialize one variable(lastGood) to null
  • Initialize one variable(first) to the head
  • Check the first element on the linked list(current)
    • If it is not the number you are looking for assign lastGood=current and move current to the next node
    • If it is the number then check if lastGood has a value assigned
      • if lastGood doesn’t have a value it means we want to remove the first item so assign first to current.next
      • if lastGood has a value then assign lastGood.next to current.next
  • Repeat for all elements
Read More

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