Airbnb's phone screening question for an engineering position

This question I've heard from friend of mine who, recently, had a phone interview with AirBnB.

Here is the question:

Total booking price = base price + service fee + cleaning fee + ...

Create a function which satisfy following condition:
Input: An array of decimals ~ X
Output: An array of integers ~ Y

sum(Y) = round(sum(x))
minimize the value of (|y1-x1| + |y2-x2| +… + |yn-xn|)

Example 1:
input = [0.3, 0.5, 0.6, 0.8, 0.9, 2.1, 3.4]
output = [ 0, 1, 1, 1, 1, 2, 3 ]

Example 2:
input = [0.5, 7.6, 47.4]
output = [ 1, 8, 47 ]


I've decided to solve it in my free time using Javascript, here is my solution:



//let numbers = [0.3, 0.5, 0.6, 0.8, 0.9, 2.1, 3.4]; // input
//let numbers = [0.7, 0.7, 0.8];
//let numbers = [0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1];
//let numbers = [3.7, 1.7, 0.8];
let numbers = [0.5, 7.6, 47.4];

function roundNumbers(input) {
  let flooredNumbers = input.map(function(a){return Math.floor(a)});
  let sumLeft = Math.round(sum(input)) - sum(flooredNumbers);
  let sorted = [];
  input.forEach(function(a,i){
    sorted.push({
      idx: i,
      elm: (a-Math.floor(a))
    })
  });
  sorted.sort(function(a,b){ return b.elm - a.elm});
  let i = 0;
  while(i < sumLeft) {
    flooredNumbers[sorted[i].idx] += 1;
    i++;
  }
  return flooredNumbers;
}

function sum(arr) {
  return arr.reduce((a, b) => a + b, 0);
}

roundNumbers(numbers);

Twisted logic: understanding truthy and falsy

An upcoming article I'll be doing for Smashing Magazine will look at the oddities and gotchas of Javascript — and lordie does it have a few.

One of these is the concept of truthy and falsy. These are sort of like true/false-lite, which will anger you somewhat if you majored in logic or philosophy. You mean you didn't know true and false were in fact gradable, not absolute concepts!?

In Javascript, every value has an in-built flag denoting its boolean equivalent. This is called upon when you ask the value to behave like a boolean, e.g. by comparing it to a real boolean. Javascript coerces your value into a boolean, such that the comparison is possible (after all, you can't compare apples with pears). See below for more on data coercion. So:

var someVar = 0;
alert(someVar == false); //evaluates true

Since we are attempting to compare zero to a boolean, Javascript coerces the zero to its truthy/falsy equivalent — and zero is a falsy (along with null, undefined, NaN, empty strings and false — everything else is a truthy). So the expression effectively becomes:

alert(false == false); //evaluates true, of course


It's important to realise that this does not mean your value is a boolean — it simply means Javascript temporarily converts it to a boolean to make the comparison possible. If we did the same comparison with the === operator (which compares not only by value but also by type), the result would be different:

alert(someVar === false); //evaluates false - someVar is a number, not a legitimate boolean


It gets worse

Clear enough? It gets more complex.

A well known quirk of Javascript is that an empty array appears to be equal to false.


( Read more )

Have you ever wondered how does look the computation of Fibonnaci number in assembly?

Have you ever wondered how does look the computation of Fibonnaci number in assembly?

For sake of curiosity I chose Pep/8 Assembler and Simulator which could be found on http://code.google.com

The original C uncached version looks as following:


#include <iostream>
using namespace std;
int fib (int n)
{
    if (n == 0)
    {
        return 0; 
    }
    else if (n == 1)
    {
        return 1;
    }
    else
    {
       return fib (n - 1) + fib (n - 2);
    }
}
int main ()
{
    int num;
    cout << "Which Fibonacci number? ";
    cin >> num;
    cout << "The number is " << fib (num) << endl; 
    return 0;
}


The Pep/8 implementation:


( Read more )

Removing dangling commits and those reachable from the reflogs

If you see something like that:
$ git fsck --lost-found
dangling commit 9dbd85d427b32773aee016efc7912f9d1c39aac4
dangling commit 604948146be1bdc2181962543cca039c18cc06b4


You might want to get rid of those dangling commits, in order to accomplish it, do following:
git reflog expire --expire=now --all #Dangerous command, it will be very difficult to recover stuff later on, if you execute it
git gc --prune=now

Top 50 Programming Quotes of All Time


50. «Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the universe trying to build bigger and better idiots. So far, the universe is winning.»
— Rick Cook

49. «Lisp isn't a language, it's a building material.»
— Alan Kay.

48. «Walking on water and developing software from a specification are easy if both are frozen.»
— Edward V Berard

47. «They don't make bugs like Bunny anymore.»
— Olav Mjelde.

46. «A programming language is low level when its programs require attention to the irrelevant.»
— Alan J. Perlis.

45. «A C program is like a fast dance on a newly waxed dance floor by people carrying razors.»
— Waldi Ravens.

44. «I have always wished for my computer to be as easy to use as my telephone; my wish has come true because I can no longer figure out how to use my telephone.»
— Bjarne Stroustrup

43. “Computer science education cannot make anybody an expert programmer any more than studying brushes and pigment can make somebody an expert painter.”
— Eric S. Raymond

42. “Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.”
— Mosher’s Law of Software Engineering

41. “I think Microsoft named .Net so it wouldn’t show up in a Unix directory listing.”
— Oktal


( Read more )

Chrome of your dream

Each of us has your-own favorite browser, that we're using in our regular life. But sometimes we just need a good solution for a portable browser that we can run from our USB flash-drive (aka disk-on-key). Today I'll tell how to make a portable build of Chrome.

1. We need to download the last available build (chrome-win32.zip) of Chromium. You can find it here:
build.chromium.org/buildbot/snapshots/chromium-rel-xp/?C=M;O=D

2. Create a new folder «Chrome»

3. Unzip chrome-win32.zip to this folder. The path to chrome.exe must be something like «..\Chrome\chrome-win32»

As I told above, we want to build a portable build of browser that could work from USB-drive at any PC. Because Windows doesn't support relative path in shortcuts (LNK-files) we have to create a separate cmd-file with startup options as command line parameters.



( Read more )

Insertion sort implemented in Javascript


   function insertSort(array){
	   for(i=1, n=array.length; i<n; i++){
   	        key = array[i];
                j=i-1;
                while(j > -1 && key < array[j]){
                     array[j+1]=array[j];
                     j--;
                }
                array[j+1]=key;
	   }
          return array;
   }


Insertion sort on Wikipedia

How to start Virtualbox after kernel upgrade

If after installing a new kernel you may get an error message when you try to start Virtualbox saying something like ‘The vboxdrv kernel module was either not loaded…’.

After the new kernel update on the Ubuntu machine, you must install the kernel headers matching the new kernel:

sudo apt-get install linux-headers-$(uname -r)

Then just run virtualbox setup

sudo /etc/init.d/vboxdrv setup

That`s all.

SQL - Update Select (Update a Table Based on Values in another Table)

A simple and quick snippet of SQL to update table values based on the values stored in another table — in other words, UPDATE SELECT. This code has only been tested in MySQL, similar code may be used in MSSQL or other database engines which I will try and add later.

UPDATE SELECT using JOIN Model:


UPDATE Dest
SET    Dest.Address1 = Src.Address1,
       Dest.Address2 = Src.Address2
FROM   SourceTableName Src
JOIN   DestinationTableName Dest ON Dest.PersonID = Src.PersonID



UPDATE SELECT using WHERE Model:


UPDATE DestinationTableName Dest, SourceTableName Src
SET    Dest.Address1 = Src.Address1,
       Dest.Address2 = Src.Address2
WHERE  Dest.PersonID = Src.PersonID

Confluence+Tomcat+HTTPD(handling HTTPS traffic, with Tomcat backstage) - The easiest way to install Confluence with Tomcat and Httpd!

Confluence is the best Enterprise wiki IMHO.
So that's how you can install and run it -
--------------------------------------------

JAVA INSTALLATION:
------------------
1.

# cp jdk-6u18-linux-x64.bin /srv/
# cd /srv
# sh jdk-6u18-linux-x64.bin
# ln -s jdk1.6.0_18 jdk

# updatedb;locate javac |grep bin
/srv/jdk1.6.0_18/bin/javac
2.
Here /srv/jdk is the actual JAVA_HOME for your machine. Note this as you will need it to run the future commands.

alternatives --install /usr/bin/java java /srv/jdk1.6.0_18/bin/java 100
alternatives --install /usr/bin/jar jar /srv/jdk1.6.0_18/bin/jar 100
alternatives --install /usr/bin/javac javac /srv/jdk1.6.0_18/bin/bin/javac 100


( Read more )