Open In App

Aartas Care Private Limited Interview Experience for Senior PHP Developer

Last Updated : 28 Jan, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

Following are some programs I was asked for a Senior Developer role.

1. Given a list of integers and two integers m and n, insert m at each nth position and print the resulting array.

PHP




<?php
  
    /*
        Take as input the following three items:
        1. The array of integers
        2. The interval after which the insertions should take place
        3. The value to be inserted
    */
      
    $array = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12];
    $position = 4;
    $current_position = $position;
    $insert = 99;
  
    /* 
        Define the Iterator    
    */
    $j = 0;
  
    /*
        Store the length of the input array in a varaible
    */
    $arr_length = count($array);
      
    /*
        Loop over the entire array using $j as the iterator variable
        If the index is divisible by the interval specified in the 
        input, Insert the value using the array_splice() function.
        Update the interval by going forward interval times in the
        array.
    */
    for($j = 0; $j < $arr_length; $j++) {
        if($j%$position==0) {
            array_splice($array,$current_position,0,$insert);
            $current_position+=$position+1;
            $j++;
        }
    }
      
    $new_count = count($array);
      
    /*
        Remove the trailing insertion
    */
    if($position != 0 && $array[$new_count - 1] == $insert && $arr_length % $position > 0) {
        array_pop($array);
    }
      
    /*
        Print the modified array to the output
    */
    echo json_encode($array);
?>


2. Given an array of consecutive odd numbers in increasing order and an integer m, find the sum of m consecutive elements in the array starting from index m

Naive Method:

The following method is naive and it takes O(m) time. In the following method, we start at the input index and add up the consecutive odd numbers in a for loop which obviously runs m times. This method is good for small data sets but it does not scale very efficiently.

PHP




<?php
  
    $array1 = array(1, 3, 5, 7, 9);
    $m = 2;
    $sum = 0;
    for($i = $m - 1; $i < $m - 1 + $m; $i++) {
        if(isset($array1[$i])) {
            $sum += $array1[$i];
        }
    }
    echo json_encode($sum);


To minimize the time taken by the program, note down a interesting observation that the output for any input is the cube(m) => m * m * m.

This method runs in constant time i.e., O(1) time

PHP




<?php
  
    $array1 = array(1, 3, 5, 7, 9);
    $m = 2;
    $sum = $m * $m * $m;
      
    echo json_encode($sum);
?>


3. What is clamp() function in CSS?

The clamp() CSS function clamps a middle value within a range of values between a defined minimum bound and a maximum bound. The function takes three parameters: a minimum value, a preferred value, and a maximum allowed value.

Example:

Parameters

The clamp(min, val, max) function accepts three comma-separated expressions as its parameters.

min => The minimum value is the smallest (most negative) value. This is the lower bound in the range of allowed values. If the preferred value is less than this value, the minimum value will be used.

val => The preferred value is the expression whose value will be used as long as the result is between the minimum and maximum values.

max => The maximum value is the largest (most positive) expression value to which the value of the property will be assigned if the preferred value is greater than this upper bound.

CSS




div {
    font-size: clamp(1em, 24px, 10em);
}


4. How to by-pass the CSRF token verification for a set of URLs in Laravel?
There is a class in the app/Http/Middleware called VerifyCsrfToken.php and it has a protected prperty named except which accepts the array of URLs to by-pass CSRF Token verification. CSRF Token Verification is sometimes a hindrance for third-party websites to redirect back to the website which intiated the process, i.e., Our hosted website.

5. What is Redis?

Redis is a cache system shipped in with Laravel in both Windows and Linux Operating systems.

It is a key-value store which scales up very efficiently. It has a set of about 50 commands for various tasks.

The main commands are:

1. GET key 2. PUT key value 3. PEEK value 4. DELETE key

There are three additional functions in Laravel for Redis:

1. Cache::remember(“key”, “value”, Closure,expiry);

2. Cache::rememberForever(“key”, “value”, Closure);

3. Cache::forget(“key”)

In the first two functions, the Closure function is executed when the key-value pair is inserted in the Redis database. The only difference between remember and rememberForever is that the Cache item is automatically deleted after the specified time.

Every key in Redis has a prefix like shopmonk:order_count. Here, shopmonk is the prefix.

Redis, by default, runs on port number 6379.

The command to clear the cache in Laravel is: – $ php artisan cache:clear

6. What are the advantages of SASS over CSS?
SASS has the following advantages over CSS

In SASS there no line terminators like “;” and no curly braces. Hence, it saves a lot of time coding and memory storage

If a particular color is to be changed, in normal CSS3 it must be changed at multiple places which is tedious and cumbersome. So, SASS has introduced the concept of variables and if any changes happen, it needs to be corrected only in one place, thus, preventing event propagation. Sometimes, we need to include a generic stylesheet inside a specific one. It can be done using @import directive. The @mixin directive accepts arguments and contains block of code that are repetitive like functions in any programming language. SASS has the concept of nested styles like styles for the most generic and nested selectors inside the Generic block. SASS also has the concept of Partials

By default, Sass transpiles all the .scss files directly. However, when you want to import a file, you do not need the file to be transpiled directly. Sass has a mechanism for this: If you start the filename with an underscore, Sass will not transpile it. Files named this way are called partials in Sass.So, a partial Sass file is named with a leading underscore.

7. How to have border of an element as image?

CSS




div {
  border-image: -webkit-linear-gradient(yellow, orange);
  border-image: -o-linear-gradient(yellow, orange);
  border-image: -ms-linear-gradient(yellow, orange);
  border-image: -moz-linear-gradient(yellow,orange);
  border-width: 5px
}


8. How to change the display from inlineblock to block in CSS on mouse over

CSS




div {
  display: inline-block
}
div:hover {
  display: block;
}




Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads