Skip to main content

Posts

PHP code for finding distinct elements common to all rows of a matrix in O(n) time complexity

<?php class Matrix{ /** •@param 2D array $matrix • •Prints distinct elements common to all rows of the matrix */ public static function getDistinctElementsCommonToAllRows($matrix){ // A hash map to store count of elements $hashmap = array(); $selectedHash = array(); $rows = count($matrix); $cols = count($matrix[0]); for ($i = 0; $i < $rows; $i++) { // Increment the count of first // element of the row if(array_key_exists($matrix[$i][0],$hashmap)){ $hashmap[$matrix[$i][0]] = $i+1; } // Starting from the second element // of the current row for ($j = 1; $j < $cols; $j++) { // If current ele
Recent posts

Write a function that checks if a given word stored in a doubly linked list is a palindrome.

<?php class Node { public $value; public $next = null; // next node public $prev = null; // previous node public function __construct($value) { $this->value = $value; } } class Palindrome { /** •@param string $word •@return bool */ public static function isPalindrome($head, $tail){ if ($head == null) return true; while ($head != $tail){ if ($head->value != $tail->value) return false; $head = $head->next; $tail = $tail->prev; } return true; } } $head = new Node(1); $firstNode = new Node(2); $secondNode = new Node(3); $tail = new Node(4); $head->next = $firstNode; $firstNode->prev = $head; $firstNode->next = $secondNode; $secondNo

PHP code for finding Longest Repeated Substrings (LRS)

<?php Class LRS{ /** •@param array $texts •Prints longest repeated substrings for each text */ public static function getAllLRS($texts){ $stringArr = array(); foreach($texts as $string){ $stringArr[] = self::LongestRepeatedSubstring($string); } return $stringArr; } public function LongestRepeatedSubstring($string){ if ($string == null) return null; $string_length = strlen($string); $substrings = array(); for ($i=0; $i < $string_length; $i++){ $substrings[$i] = substr($string, $i); } sort($substrings); $result = ""; for ($i = 0; $i < $string_length - 1; $i++){ $lcs = self::LongestCommonString($substrings[$i], $substrings[$i + 1]);

PHP code for Implementing LRU cache.

<?php interface LRUCache{ /** •@param string $key •@param string $value •@return bool $result • •Stores value against the key in the cache */ public function insertIntoCache($key,$value); /** •@param string $key •@return string $value •Gets the value of a key from the cache */ public function getFromCache($key); /** Purge the entire cache */ public function purgeCache(); /** •@return int $count •Gets the number of successful cache hits so far */ public function allCacheHits(); /** •@return int $count •Gets the number of unsuccessful cache hits so far **/ public function allCacheMissed(); } class Cache implements LRUCache{ // int the max number of elements the cache supports private $capacity; // Array representing a naive hashmap (TODO needs to pass t

PHP code to find the diameter of tree in O(n) time complexity

<?php class BinaryNode{ public $value = null; // node value public $left = null; // left child public $right = null; // right child public function __construct($value) { $this->value = $value; } } class BinaryTreeDiameter{ //find the diameter and print its value public function findDiameterOfTree($root){ //Checking node is empty or not if($root === null){ return 0; } // Compute the depth of each subtree $lDepth = $this->findDiameterOfTree($root->left); $rDepth = $this->findDiameterOfTree($root->right); // Return the greater one. if ($lDepth > $rDepth) return $lDepth+1; else return $rDepth+1; } //find the diameter and print its

Wanna Cry - A Ransomware

WANNA CRY ( A Ransomware ), the popular word today.  Ransomware is a malicious application which can be triggered by clicking on a phishing mail, image, docs, etc. and it will block the access of your data until a certain amount is not paid. And display a message for payment to unblock it. its basically based on cryptography. It will encrypt the victim files.  Payment should be in digital currency Bitcoin(1 Bitcoin=113190.04 Indian Rupee). They are giving you a time period in which you have to make the payment to take files back. After run-out of the time the amount increases and then again a time window popup. But after passing that time window, if the payment is not done, then you will lost your access to the files permanently. The concept of file-encrypting ransomware was invented and implemented by Young and  Yung  at  Columbia University  and was presented at the 1996 IEEE Security & Privacy conference. It is called  cryptoviral extortion  and is the following 3-roun

What Make Facebook page load faster ?

Facebook has a “ Lazy Loading ” system they call “ BigPipe ” that helps the pages load fast. Facebook breaks each page down into sections they call “ Pagelets” , and using Javascript only load the most important Pagelets first then load the less important ones shortly afterward. And pipeline them through several execution stages inside web servers and browsers, as the modern microprocessor do to serve the various request in an order to increase the productivity. Big Pipe is totally implemented in PHP and javascript. By loading and rendering the main page structure first with minimal info on it, the page appears to load quicker than having to wait for a complete page to download and then render. To exploit the parallelism between web server and browser, BigPipe first breaks web pages into multiple chunks called pagelets. Just as a  pipelining microprocessor  divides an instruction’s life cycle into multiple stages (such as “instruction fetch”, “instruction decode”, “executio