Best short notes on programming concepts & questions asked in interviews

Share sandboxes with short explanation with the community

Post your first short note
# All# python# wedfg# scope variable# interview# lexical environment# javascript# #algorithm# hoisting# print# sample# #c# gambling# primitives# algoritms# arrow-functions# javascriptintro# usefullinks

Latest from community today

// This is what a lexical environment conceptually look like:
LexicalEnvironment = {
  Identifier:  <value>,
  Identifier:  <function object>
}

//code example

function a() {
  var b = 10;
  c();
  function c() {
    console.log(b); //it prints the right value. How? See ans below Summary part
  }
 }
 
 a();
 console.log(b); // now when cursor comes here, it prints NOT DEFINED!

/*
function c is lexically inside function a. 
- So in EC of c(), variables and fun in c (none) + reference of lexical env of parent a() is there
- LE of a() in turn is its memory space + reference to LE of parent (Global EC)
- LE of Global EC points to its memory space + *NULL* (as no parent for Global EC).

*/
//JavaScript only hoists declaration not initialization
// code example 1

var x = 7;
function getName(){
    console.log("Hii JavaScript");
}

getName();
console.log(x);

/*
Output:

>  Hii JavaScript
>  7
*/

// code example 2

// in most languages, both lines which are below their declaration will give error. Not in JS though. 
getName(); 
console.log(x); 

var x = 7;
function getName(){
    console.log("Hii JavaScript");
}

/*
Output:
>  Hii JavaScript
>  undefined
*/


// code example 3

getName();
console.log(x);

function getName(){
    console.log("Hii JavaScript");
}


/*
Output:

>  Hii JavaScript

>  Error: x is not defined   // note that not defined here and "undefined" in sample 2 are totally different.

- Not defined: We have not initialised the value for variable anywhere in the entire code and in memory space. 
- Undefined: 

__Hoisting__ is a concept which enables us to extract values of variables and functions even before initialising/assigning value without getting *error*

*/

// code example 4

function getName(){
    console.log("Hii JavaScript");
}

console.log(getName)

/*
Output:

> f getName(){
      console.log("Hii JavaScript");
  }
*/

// code example 5

getName();
console.log(x);
console.log(getName)

var x = 6;
function getName(){
    console.log("Hii JavaScript");
}


/*

Output:
>  Hii JavaScript

>  undefined

>  f getName(){
      console.log("Hii JavaScript");
  }
*/

// code example 6 : using arrow function

console.log(getName)

var getName = function () {
    console.log("Hii JavaScript");
}

var getName = () => {  // use fat arrow function  
    console.log("Hii JavaScript");
}

/*

Output:

>  undefined //it is because they behave as variable and not function. 

*/

/*
But why undefined?

When JavaScript engine finds a var variable declaration during the compile phase, it will add that variable to the lexical environment and initialize it with undefined and later during the execution when it reaches the line where the actual assignment is done in the code, it will assign that value to the variable.
*/
void sort(){

   int i, j, k, tempKey, tempData ;
   struct node *current;
   struct node *next;
   int size = length();
   k = size ;
	
   for ( i = 0 ; i < size - 1 ; i++, k-- ) {
      current = head ;
      next = head->next ;
		
      for ( j = 1 ; j < k ; j++ ) {
		
         if ( current->data > next->data ) {
            tempData = current->data ;
            current->data = next->data;
            next->data = tempData ;

            tempKey = current->key;
            current->key = next->key;
            next->key = tempKey;
         }
			
         current = current->next;
         next = next->next;                        
      }
   }   
}
<?php

$names = ['Petro', 'oleg', 'zanar', 'yura', 'igor'];

sort($names);

function find($names, $search) {
    $count = count($names);
    
    $half = floor($count / 2);
    $element = $names[$half];
    
    if ($element === $search) return $element;
    if ($count === 1) return false; 
    
    if ($search > $element) {
        return find(array_slice($names, $half), $search);
    } else {
        return find(array_slice($names, 0, $half), $search);
    }

}

echo find($names, 'Petro');
<?php

const MAX_ROUND = 100, MUL = 2,CREDIT_INIT = 200;
$current_balance = CREDIT_INIT;$bet_amount = 1;
$win_count = 0;$lose_count = 0;
$turn_over = 0;$total_bet_win = 0;$total_bet_lose=0;


for($i = 0;$i<MAX_ROUND;$i++)
{
    $result = mt_rand()%2;$turn_over += $bet_amount;
  
  	if($result == 1)
    {
      $current_balance = $current_balance+$bet_amount;$total_bet_win += $bet_amount;
      $win_count++; $bet_amount = 1;     
    }
  	
  	else
    {
      $current_balance = $current_balance-$bet_amount;$total_bet_lose += $bet_amount;
      $lose_count++;$bet_amount = $bet_amount*MUL; 

    }
  
  
  
        $result = $result == 1 ? 'win' : 'lose';
    	echo "round :".(1+$i)." result: ".$result ."(W/L) :"."(".$win_count."/".$lose_count.")";
        echo " ,bet_amount :".$bet_amount." ,balance : ".$current_balance."\n";
}
		echo "\n\n|___________________SUMMARY________________|\n";
		echo "Turnover: "."$turn_over". "\nTotal Win: ".$total_bet_win."\nTotal Lose: ".$total_bet_lose;
		echo "\nProfit: ". ($current_balance - CREDIT_INIT);



Featured content