Category Archives: Application Design

OAuth2

Oauth2 is an authentication method where you allow clients to access resources in a server by authenticating in a different server. I am building a system where I will need this infrastructure so I will do my best to explain how to build and use an Oauth2 server.

The components

  • Resource owner: This is a person. Lets call him Adrian
  • Resource server: This is a server where Adrian’s information lives (along with other people’s information). The resource server needs to show Adrian only his information. We’ll call this app server
  • Client: This can be a browser or an app that Adrian uses to interact with the app server. This is the browser
  • Authorization server: This is our Oauth server. It validates user credentials and assigns tokens among other things. We’ll call this one oauth server

Read more »

UUIDs

UUID stands for Universally Unique ID. It is a 128-bit value that is usually represented by hexadecimal characters divided by dashes:

1
b54c9b1a-e19c-44e7-ab81-9528c195da02

They are called Universally Unique because in practice it is very hard to have collisions even if two(or more) independent systems generate these IDs independently. It is of course possible to have collisions, but the chances of it are low enough that it can be treated as impossible in most scenarios.

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 »

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 »

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 »

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 »