Skip to content

Instantly share code, notes, and snippets.

@kaizokuace
Last active May 28, 2017 18:28
Show Gist options
  • Select an option

  • Save kaizokuace/1b6c81da262c90bba41fec43a41f497e to your computer and use it in GitHub Desktop.

Select an option

Save kaizokuace/1b6c81da262c90bba41fec43a41f497e to your computer and use it in GitHub Desktop.
Appetize programmer test

LAMP Assessment

Write a PHP function that takes a sentence and returns the word with the most vowels.

Answer:

<?php
function mostVowels($sentence) {
    $sentence = preg_replace("/[^A-Za-z0-9 ]/", '', $sentence);
    $words = explode(' ', $sentence);
    $maxWord;
    $maxCount = 0;
    foreach($words as $word) {
        preg_match_all('/[aeiou]/i',$word, $matches);
        $curCount = count($matches[0]);
        if ($curCount > $maxCount) {
            $maxCount = $curCount;
            $maxWord = $word;
        }
    }
    return $maxWord;
}
What is the final output of the below code?
<?php
function changeName(&$newName) {
  $newName = 'blah';
}
function changeAgain($newName) {
  $newName = 'Keith';
}
$myName = 'Keith';
changeName($myName);
changeAgain($myName);
echo $myName;

Answer: It echos 'blah', changeName takes a reference and changeAgain passes by value.

How would you approach refactoring the following class in order to make it more testable and clean?
<?php
class SomeImportantClass {
  public function someFunction($someParameter, $otherParameter) {
    $someData = SomeSingleton->getInstance()->calculateStuff($otherParameter);


    $result = ... // operate on $someData and $someParameter


    return $result;

  }
}

Answer:

<?php
class SomeImportantClass {
  protected $singleton;
  public function __construct($singleton=SomeSingleton) {
    $this->singleton = $singleton;
  }
  public function someFunction($someParameter, $otherParameter) {
    $someData = $this->singleton->getInstance()->calculateStuff($otherParameter);


    $result = ... // operate on $someData and $someParameter


    return $result;

  }
}

As a deeper answer, since a singleton is being used in this class, when writing tests either the singleton should implement a reset function for the test setup and teardown or reflection api can be used to reinstance the singleton. In general though, singleton pattern makes for difficult testing since you are spreading what is effectively a global scope all over the code.

Write a PHP function that returns true if a number is prime and false otherwise.

Answer:

<?php
function isPrime($number) {
  for ($i=2; $i < $number; $i++) {
    if($number % $i == 0) {
      return false;
    }
  }
  return true;
}
Consider the following MySQL database schema:
items orders delivery_address
order_iditem_namequantity
100Shirt1
101Shirt1
102Hat1
103Jacket1
101Hat2
104Hat3
103Pants2
103Shirt2
iddateamount
1002016-12-04$5.00
1012016-12-27$30.00
1022017-01-06$25.00
1032017-01-14$6.00
1042017-01-14$13.00
order_idaddress
100123 Main St
103456 First Ave
104789 Central Blvd
Write a query that retrieves order IDs that contained a Hat, and include the delivery address if provided

Answer:

SELECT id, address FROM orders
JOIN items ON orders.id = items.order_id
LEFT JOIN delivery_address ON orders.id = delivery_address.order_id
WHERE item_name = 'Hat';
Write a query that retrieves each order and the total count of all items sold for each order. Sort the results per orders having sold the highest counts of “distinct” items

Answer:

SELECT i.order_id, i.sum AS total_count
FROM (
  SELECT order_id, count(order_id), sum(quantity) FROM items
	GROUP BY order_id
	ORDER BY count DESC
) i
Consider the two arrays, $carMakeCount and $carMakeType. Write a function that maps both datasets and returns an array having a structure identical to $exampleMapped:
<?php
$carMakeCount = ['lexus' => 4, 'bmw' => 2, 'mercedes' => 5];
$carMakeType = ['suv'=>'lexus', 'coupe'=>'mercedes', 'sedan'=>'bmw'];

$exampleMapped = [
	'lexus has 4 suv',
	'bmw has 2 sedan',
	'mercedes has 5 coupe'
];

Answer:

<?php
function exampleMap($carMakeCount, $carMakeType) {
	return array_map(function ($make, $type) {
	  return "$make has $carMakeCount[$make] $type";
	}, $carMakeType, array_keys($carMakeType));
}
Design a set of PHP classes in an object oriented manner to model various shapes. Start with a Shape class where a shape is any two dimensional figure that has the following characteristics: name, perimeter, area.

Answer:

<?php
interface IShape {
  public function area();
  public function perimeter();
}

interface IStringable {
  public function toString();
}

class Shape {
  protected $name;
  public function __construct($name) {
    $this->name = $name;
  }
  public function name() {
    return $this->name;
  }
}
Define a Circle that share the same characteristics defined for a Shape
<?php
class Rectangle extends Shape implements IShape, IStringable {
  protected $width;
  protected $height;

  public function __construct($width, $height, $name="Rectangle") {
    parent::__construct($name);
    $this->width = $width;
    $this->height = $height;
  }

  public function area() {
    return $this->width * $this->height;
  }
  public function perimeter() {
    return 2 * ($this->width + $this->height);
  }
  public function toString() {
    return "[$this->name] width: $this->width, height: $this->height";
  }
}

class Circle extends Shape implements IShape, IStringable {
  protected $radius;

  public function __construct($radius, $name="Circle") {
    parent::__construct($name);
    $this->radius = $radius;
  }

  public function area() {
    return $this->radius * $this->radius * pi();
  }
  public function perimeter() {
    return 2 * pi() * $this->radius;
  }
  public function toString() {
    return "[$this->name] radius: $this->radius";
  }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment