Categories

API Question

Categories

## How to fibonacci with PHP ? Recursively

This function runs in O(2 exponent N)

``````function fibonacci(int \$n) {
if (\$n == 0) {
return 0;

}
if (\$n == 1) {
return 1;
}

return fibonacci(\$n-1) + fibonacci(\$n-2);
}

echo 'FINAL ' . fibonacci(9);``````
Categories

## How to Fibonacci Bottom-Up Memoization PHP ? Dynamic Programming

This function runs in O(n) time

``````<?php
function fibonacci(int \$n) {
if (\$n == 0) {
return 0;

}    else if (\$n == 1) {
return 1;
}

\$memo = array_fill(0, \$n, 0);
\$memo = 0;
\$memo = 1;

for (\$i = 2; \$i < \$n; \$i++) {

\$memo[\$i] = \$memo[\$i - 1] + \$memo[\$i - 2] ;
echo    "memo[\$i] (\$memo[\$i]) = memo[\$i - 1] + memo[\$i - 2] \n\r";

}
return \$memo[\$n - 1] + \$memo[\$n - 2];
}``````

OR

``````function fibonacci(int \$n) {
if (\$n == 0) {
return 0;

}    else if (\$n == 1) {
return 1;
}

\$memo = array_fill(0, \$n, 0);
\$a = 0;
\$b = 1;

for (\$i = 2; \$i < \$n; \$i++) {

\$c = \$a + \$b ;
\$a = \$b;
\$b = \$c;
echo    "c (\$c) = a (\$a) + b (\$b)  \n\r";

}
return \$a + \$b;``````
Categories

## How to Fibonacci Top-Down Memoization PHP ?

Top-Down Memoization PHP Fibonacci Problem

``````function fibonacci1(int \$n) {

\$result = array_fill(0, \$n + 1, 0);
<?php
function fibonacci1(int \$n) {

\$result = array_fill(0, \$n + 1, 0);
return fibonacci(\$n,\$result );
}

function fibonacci(int \$i, \$memo) {

if (\$i == 0 || \$i == 1) return \$i;

if (\$memo[\$i] == 0) {
\$memo[\$i] = fibonacci(\$i - 1, \$memo) + fibonacci(\$i - 2, \$memo);
}

echo " memo[\$i] ".\$memo[\$i]."\n\r";

return \$memo[\$i];
}

echo 'FINAL ' . fibonacci1(9);``````

Categories

## Big O notation Chart

Speed performance is the key of any successful application – use the following charts for understanding the big O nation and for upcoming FAANG interviews

source https://jarednielsen.com/big-o-notation/

Also do not hesitate to prepare your interviews with this book

Categories

## DEVELOPER Methodologies to follow

MethodologyDescription
DRYDo not Repeat Yourself
KISSKeep It Studid Simple
SOLIDSolid Principles
Big O NotetationDo not Repeat Yourself
Function not bigger than 30 linessmaller function to make code more readable
PSR ImplementationDescription
PSR-2Coding Style Guide
PSR-3Logger Interface