Example of Associative Array in Javascript

Roy wanted to increase his typing speed for programming contests. So, his friend advised him to type the sentence “The quick brown fox jumps over the lazy dog” repeatedly, because it is a pangram. (Pangrams are sentences constructed by using every letter of the alphabet at least once.)

After typing the sentence several times, Roy became bored with it. So he started to look for other pangrams.

Given a sentence , tell Roy if it is a pangram or not.

E.g: We promptly judged antique ivory buckles for the next prize

function processData(input) {
    //Enter your code here
    var inpArr = input.split("");
    var uinput = input.toUpperCase();
    var freqObj = {};
    for (var a=65; a <= 90 ; a++) {
        var s = String.fromCharCode(a);
        freqObj[s] = 0;  // associative array as an object
    for (var i = 0; i < uinput.length ; i++) {
        var char = uinput[i];
        //if (char in freqObj) {
        if ((char in freqObj) && (freqObj.hasOwnProperty(char)) ) {
            freqObj[char] = 1;
    var okeys = Object.keys(freqObj);
    var sum = 0;
    for (var k = 0; k < okeys.length; k++) {
        if (freqObj.hasOwnProperty(okeys[k])) {
            sum += freqObj[okeys[k]];
    if (sum == 26) {
    } else {
        console.log("not pangram");

_input = "";
process.stdin.on("data", function (input) {
    _input += input;

process.stdin.on("end", function () {

PHP Reading from command line

$handle = fopen ("php://stdin","r");
// if single input (having no spaces)
// if trying to read 2 values separated by a space
fscanf($handle,"%d %s",$d,$s);
// if trying to read a string having spaces
$inp = trim(fgets($handle));

Advanced Features of PHP

1) Late Static Binding:
“Late binding” comes from the fact that static:: will not be resolved using the class where the method is defined but it will rather be computed using runtime information.

class A {
    public static function who() {
        echo __CLASS__;
    public static function test() {

class B extends A {
    public static function who() {
        echo __CLASS__;


Instead of “self::who()” replace with “static::who()”. Then the output will be “B” instead of “A”.

2) Variable Length Argument Lists :

func_num_args() ==> returns number of arguments passed
func_get_arg(int) ==> returns the nth argument passed in
func_get_args() ==> returns an array of all arguments

New Features in PHP 7


1) Speed

2) Type Declarations:
Since PHP 5, you can use type hinting to specify the expected data type of an argument in a function declaration, but only in the declaration. When you call the function, PHP will check whether or not the arguments are of the specified type. With PHP 7 we now have added Scalar types. Specifically: int, float, string, and bool.

function stringTest(string $string) {
    echo $string;

Additionally, PHP 7 gives us the opportunity to enable strict mode on a file by file basis. We do this by declare(strict_types=1); at the top of any given file.

PHP 7 also supports Return Type Declarations which support all the same types as arguments. To specify the return type, we add a colon and then the type right before the opening curly bracket.

function getTotal(float $a, float $b) : float {

3) Error Handling :
In PHP 7, an exception will be thrown when a fatal and recoverable error occurs, rather than just stopping the script. Fatal errors still exist for certain conditions, such as running out of memory, and still behave as before by immediately stopping the script. An uncaught exception will also continue to be a fatal error in PHP 7. This means if an exception thrown from an error that was fatal in PHP 5 goes uncaught, it will still be a fatal error in PHP 7.

4) New Operators (Spaceship Operator, Null Coalesce Operator) :

Spaceship Operator (useful in sort algorithms, etc)

$compare = 2 <=> 1
2 < 1? return -1
2 = 1? return 0
2 > 1? return 1

the Null Coalesce Operator, is effectively the fabled if-set-or.
If $firstName is empty then set it to Guest.

$name = $firstName ??  "Guest";

Can be chained.

$name = $firstName ?? $username ?? $placeholder ?? “Guest”; 

5) Anonymous Classes :

Anonymous classes are useful when simple, one-off objects need to be created.

$util->setLogger(new class {
    public function log($msg)
        echo $msg;


Given a sequence of words, print all anagrams together

$sortFunc = function($val) {
   $strArr = str_split($val);
   return implode("", $strArr);

$arr = array("cat", "dog", "tac", "god", "act");

$words = array();
$index = array();

// form a new array with each string sorted within itself
$words = array_map($sortFunc, $arr);
// form a index array in order to remember original index
foreach ($words as $key => $val) {
    $index[$key] = $val;
asort($index);  // sort the values but maintain index association
foreach ($index as $key => $val) {
    // use index value in the original array to print
    echo $arr[$key] . "\n";

Given a sequence of words, print all anagrams together | Set 1

Interesting Algorithm Interview Questions

1) Sorting a list of words such anagrams are grouped together. (Soundhound.com)
Input: [‘abc’,’test’,’vac’, ‘bac’, ‘london’, ‘cba’, ‘cav’, ‘lon’, ‘pst’]
Output: [‘abc’, ‘bac’, ‘cba’, ‘vac’, ‘cav’, ‘london’, ‘test’, ‘lon’, ‘pst’]

2. Design Twitter algorithm to retrieve top 10 new feeds (Soundhound.com)

Stored Procedure vs Function in MySQL

* Stored Procedures are pre-compile objects which are compiled for first time and its compiled format is saved which executes whenever it is called. But Function is compiled and executed every time when it is called.

* Procedures are used as scripts and functions are used as methods

* Functions have a scalar return value. Procedures do not have a return value.

* A stored procedure may have arguments that are IN, OUT, or INOUT. Functions may only have IN arguments.

* To invoke a stored procedure, use the CALL statement. To invoke a stored function, refer to it in an expression

Find a substring within a string and return the index where it was found

$bigString = "I am a really big realtor in the bay area";
$pattern = "realtor";

$i = 0;
$j = 0;
$bigStringLen = strlen($bigString);
$patternLen = strlen($pattern);
$found = false;
while ($i <= ($bigStringLen - $patternLen)) {
   if ($bigString[$i] == $pattern[$j]) {

   } else {
   $i++; $j++;
   if ($j >= $patternLen) {
       $found = true;
if ($found) {
    print "found at " . ($i-$patternLen) . "\n";
} else {
    echo "\nnot found\n";