Expand a random range from 1–5 to 1–7

You have a random number generator between 1 and 5. Make a random number between 1 and 7

int rand7()
{
    int vals[5][5] = {
        { 1, 2, 3, 4, 5 },
        { 6, 7, 1, 2, 3 },
        { 4, 5, 6, 7, 1 },
        { 2, 3, 4, 5, 6 },
        { 7, 0, 0, 0, 0 }
    };

    int result = 0;
    while (result == 0)
    {
        int i = rand5();
        int j = rand5();
        result = vals[i-1][j-1];
    }
    return result;
}

Balanced Brackets

$handle = fopen ("php://stdin","r");
fscanf($handle,"%d",$t);
for($a0 = 0; $a0 < $t; $a0++){
    fscanf($handle,"%s",$str);
    $stack = array();
    $unevenFlag = false;
    $matchingP = array(
        '}' => '{',
        ']' => '[',
        ')' => '('
    );
    $closingP = array_keys($matchingP);
    $openingP = array_values($matchingP);
    for ($i=0; $i < strlen($str); $i++) {
      if (in_array($str[$i], $openingP)) {
        array_push($stack, $str[$i]);
      } elseif (in_array($str[$i], $closingP)) {
        $openP = array_pop($stack);
        if ($matchingP[$str[$i]] != $openP) {
            $unevenFlag = true;
            break;
        }  
      }
    }
    if (count($stack) != 0 || $unevenFlag === true) {
      print "NO\n";
    } else {
      print "YES\n";
    }
}

Linked List vs Array

Advantages of Linked List :
– Can be expanded in constant time. Dynamic allocation of storage. To create array we must allocate memory for certain number of elements. To add more elements we must create a new array and copy the old array into nw.
Insertion at beginning of linked list is O(1)

Disadvantages of Linked List:
– Access time to individual elements O(n). Array takes O(1)
– Arrays have “Locality in memory”. Arrays are contiguous blocks of memory. Any array element will be physically near its neighbors. This benefits from modern CPU caching mechanisms. Linked List elements can be scattered.
– Linked List are harder to manipulate. You need to change pointers if deleting from middle or end, etc.
– Wasted memory in terms of extra reference points

Explain FLUX

Flux is an architectural pattern that enforces unidirectional data flow.

MVC vs Flux:
MVC did not scale well for Facebook’s huge codebase. The main problem for them was the bidirectional communication, where one change can loop back and have cascading effects across the codebase (making things very complicated to debug and understand).

How does Flux solve this? By forcing an unidirectional flow of data between a system’s components.
In general the flow inside the MVC pattern is not well defined. A lot of the bigger implementations do it very differently (e.g. Cocoa MVC vs. Ruby on Rails MVC).
Flux on the other hand is all about controlling the flow inside the app?—?and making it as simple to understand as possible.

Action –> Dispatcher –> Store –> View

Facebook found that two-way data bindings led to cascading updates, where changing one object led to another object changing, which could also trigger more updates. As applications grew, these cascading updates made it very difficult to predict what would change as the result of one user interaction. When updates can only change data within a single round, the system as a whole becomes more predictable.

https://medium.com/@grover.vinayak0611/what-is-flux-architecture-why-facebook-used-it-and-the-comparison-with-mvc-architecture-49c01ed5d2e1

http://blog.andrewray.me/flux-for-stupid-people/

What is the difference between array_map, array_walk, array_filter, array_reduce in PHP?

array_walk() Applies the user-defined callback function to each element of the array array. Returns a boolean. May or May not Modify the original array. To modify original array callback function can accept an argument by reference.

array_map() returns an array containing all the elements of array1 after applying the callback function to each one. The number of parameters that the callback function accepts should match the number of arrays passed to the array_map(). So array_map does not modify the original array.

array_filter() Iterates over each value in the array passing them to the callback function. If the callback function returns true, the current value from array is returned into the result array. Array keys are preserved.
Returns subset of array.

array_reduce() applies iteratively the callback function to the elements of the array, so as to reduce the array to a single value. Returns single value.

The idea of mapping an function to array of data comes from functional programming. You shouldn’t think about array_map as a foreach loop that calls a function on each element of the array (even though that’s how it’s implemented). It should be thought of as applying the function to each element in the array independently.

In theory such things as function mapping can be done in parallel since the function being applied to the data should ONLY effect the data and NOT the global state. This is because an array_map could choose any order in which to apply the function to the items in (even though in PHP it doesn’t).

array_walk on the other hand it the exact opposite approach to handling arrays of data. Instead of handling each item separately, it uses a state (&$userdata) and can edit the item in place (much like a foreach loop). Since each time an item has the $funcname applied to it, it could change the global state of the program and therefor requires a single correct way of processing the items.

Back in PHP land, array_map and array_walk are almost identical except array_walk gives you more control over the iteration of data and is normally used to “change” the data in-place vs returning a new “changed” array.

http://stackoverflow.com/questions/3432257/difference-between-array-map-array-walk-and-array-filter

Traits vs Interfaces

Interfaces : An interface defines a set of methods that the implementing class must implement.

Trait : When a trait is use’d the implementations of the methods come along too–which doesn’t happen in an Interface.

http://stackoverflow.com/questions/9205083/traits-vs-interfaces

Late Static Binding in PHP

http://php.net/manual/it/language.oop5.late-static-bindings.php

Basically, it boils down to the fact that the self keyword does not follow the rules of inheritance. self always resolves to the class in which it is used. This means that if you make a method in a parent class and call it from a child class, self will not reference the child as you might expect.

Late static binding introduces a new use for the static keyword, which addresses this particular shortcoming. When you use static, it represents the class where you first use it, ie. it ‘binds’ to the runtime class.