Skip to main content

Posts

Code Challenge : Create a PHP function to find the intersection of two arrays - FirstDue Interview Questions

 This was the client round question for code challenge.. Create a PHP function to find the intersection of two arrays. <?php function FindIntersection ( $strArr ) {     // Split the strings into arrays and convert to integers     foreach ( $strArr as $index => $element ) {         $strArr [ $index ] =   array_map ( 'intval' , explode ( ',' , $element ));     }     $main_match_array = $strArr [ 0 ];     array_shift ( $strArr );     $interarray = [];     foreach ( $strArr as   $each_chunk ) {         foreach ( $main_match_array as $in => $number ) {             if ( in_array ( $number , $each_chunk )) {                 $interarray [ $number ] = $number ;             } else {                 unset ( $int...
Recent posts

Design Patterns with PHP

Time to go through some design patterns used wide in creating the applications. Why do they exists, anyway? Let's go to Observer pattern.See the example below,  <?php // Observer interface interface Observer {     public function update($message); } // Concrete Observers class EmailSubscriber implements Observer {     private $email;     public function __construct($email) {         $this->email = $email;     }     public function update($message) {         echo "Email sent to {$this->email}: $message\n";     } } class SMSSubscriber implements Observer {     private $phone;     public function __construct($phone) {         $this->phone = $phone;     }     public function update($message) {         echo "SMS sent to {$this->phone}: $message\n";     } } // Subject class NewsPublishe...

MySQL Trigger - Testdome Code challenge

  A MySQL Trigger It is a set of SQL statements that are automatically executed  on a particular table (such as INSERT, UPDATE, or DELETE) when few event happens. Triggers are useful for handling the business logic, log the audit and modify the data on database level.   Following is the Syntax of a MySQL Trigger CREATE TRIGGER trigger_name {BEFORE | AFTER} {INSERT | UPDATE | DELETE} ON table_name FOR EACH ROW BEGIN    -- Add your SQL statements inside this block END; Lets know more with few examples.  Example 1: Audit Log on Insert Imagine if we have a users table and audit_log table.We want to log info whenever a new user is added to users table. Let's Create Tables first: CREATE TABLE users (     id INT AUTO_INCREMENT PRIMARY KEY,     name VARCHAR(100),     email VARCHAR(100) ); CREATE TABLE audit_log (     id INT AUTO_INCREMENT PRIMARY KEY,     action_time DATETIME,     action VARCHAR(100) ); Ne...

Testdome Interview question - Mysql Stored Procedure

 Mysql Stored Procedure  A company needs a stored procedure that will insert a new user with an appropriate type. Consider the following tables: TABLE userTypes id INTEGER NOT NULL PRIMARY KEY, type VARCHAR(50) NOT  NULL TABLE users id INTEGER NOT NULL  PRIMARY KEY  AUTO_INCREMENT, email VARCHAR(50)NOT NULL, userTypeId INTEGER NOT NULL ,FOREIGN KEY(userTypeId) REFERENCES  user Types (id) Finish the insertUser procedure so that it inserts a user, with these requirements: • id is auto incremented. • email is equal to the email parameter. • userTypeld is the id of the userTypes row whose type attribute is equal to the type parameter. DELIMITER $$ CREATE PROCEDURE insertUser(     IN p_email VARCHAR(50),     IN p_type VARCHAR(50) ) BEGIN     DECLARE v_userTypeId INT;     -- Get userTypeId from userTypes table     SELECT id INTO v_userTypeId  FROM userTypes WHERE type = p_type    LIMIT 1;   ...

CTE (Common Table Expressions) — SQL

Getting started: filtering your data Let's start by examining an example  payments  table. Our goal is to roll up old months and put them into a summary table. To begin, we need to filter down to just older months: SQL SELECT amount , YEAR ( payment_date ) , MONTH ( payment_date ) FROM payments WHERE payment_date < DATE_FORMAT ( CURRENT_DATE , '%Y-%m-01' ) Here, we're selecting the  amount  of the payment as well as the year and month that the payment was made. By limiting the results to  payment_date  values that are less than the first day of the current month, we ensure that we're only selecting data from previous months. Grouping your data Once we have our filtered data, we need to group it by year and month so we can roll it up into the summary table: SQL SELECT sum ( amount ) as amount , YEAR ( payment_date ) as ` year ` , MONTH ( payment_date ) as ` month ` FROM payments WHERE payment_date < ...