Interview questions. 3 simple tasks but still failed

  php

A friend has just failed a simple PHP job interview exam on devTest. The exam came with PHPUnit tests which all passed but there was a hidden test in the background which was failing. devTest just showed a window that said:

You have a bug!
Be sure to read the exercise descriptions carefully and that return values are in the requested formats.
Remember that there are hidden tests as well – and there may be no winners!

I can’t find the issue. I’m sure its a simple one. Can anyone help? (The test has been attempted and interview failed. Just trying to see where it went wrong. 🙂

<?php

class CodingFun
{
    /*
    Task #1
    
    Write a function that returns an array containing numbers within the Fibonacci sequence. 
    The size of the array will be provided as an input parameter.

    Hint: Each number in the sequence is the sum of the two numbers that precede it. E.g. 0,1,1,2,3,5, etc.
    */
    public function getFibonacciSequence($resultArraySize)
    {
      $num1 = 0; 
      $num2 = 1; 
      $FibonacciSeriesArray = []; 

      while (count($FibonacciSeriesArray) < $resultArraySize) { 
        array_push($FibonacciSeriesArray, $num1);
        $num3 = $num2 + $num1; 
        $num1 = $num2; 
        $num2 = $num3; 
      }
      return $FibonacciSeriesArray;
    }

    /*
    Task #2

    Write a function that returns an array of customers along with their account balance.

    An array containing customer names and transaction values will be provided as an input parameter. 
    
    This array will contain string values in the format 'CUSTOMERNAME=PENCE'. For example, 'JOHN=9900'.

    The return array must contain key => value pairs where the key is the customer name in LOWER case and 
    the integer value is the balance in pence (sum of transaction values).

    The array must be returned in alphabetical order based upon the key names.
    */
    public function getCustomerBalances($customerTransactions)
    {
        $keyValueCustomerTransactions = [];
        
        foreach($customerTransactions as $customerTransaction) {
          $bits = explode('=', $customerTransaction);        
          $customerNameInLower = strtolower($bits[0]);
          
          //check if key exists
          if (array_key_exists($customerNameInLower, $keyValueCustomerTransactions)) {
            // add monies to existing key
            $keyValueCustomerTransactions[$customerNameInLower]+=$bits[1]; 
          } else {
            //else create a new key in lowercase  
            $keyValueCustomerTransactions[$customerNameInLower] = $bits[1];
          }  
        }
        // Sort the array alphabetically wrt keys
        ksort($keyValueCustomerTransactions);
        
        return $keyValueCustomerTransactions; 
    }
    
    /*
    Task #3

    A six digit lottery number is drawn and compared to numbers previously selected by players. 
    Players whose six digit number matches the number in the lottery draw split the winnings equally amongst each other.

    Write a function that calculates the winnings for players participating in a lottery.

    The six digit lottery number, total winnings and an array containing player data will be provided as input parameters.

    The function should return a key => value array containing the player name for the key and the winnings for the player as the value.

    Only winning players should be returned within the result.

    The result should be in alphabetical order based upon the players name.
    */
    
    public function getLotteryWinnings($totalWinnings, $winningNumber, $players)
    {
      $winningPlayers = [];
      
      //First, get the winning players
      foreach ($players as $playerName => $lotteryNumber) {
        if ($lotteryNumber == $winningNumber ) {
          $winningPlayers[$playerName] = null;    
        } 
      }
      
      //Count the winning players and divide the win equally among them
      $totalWinningPlayers = count($winningPlayers);
      $sharedWinAmount = $totalWinnings/$totalWinningPlayers;
      foreach ($winningPlayers as $winningPlayerName => $winningPlayerValue) {
        $winningPlayers[$winningPlayerName] = $sharedWinAmount; 
      }
      // Sort the array alphabetically wrt keys
      ksort($winningPlayers);
        
      return $winningPlayers;
    }
}

The code/answers above pass all of the following tests that were written by the prospective employer:

<?php

require 'CodingFun.php';

class CodingFunTest extends PHPUnit_Framework_TestCase
{
    public function testGetFibonacciSequence()
    {
        $codingFun = new CodingFun();

        $resultArraySize = 10;

        $result = $codingFun->getFibonacciSequence($resultArraySize);

        $this->assertEquals([0, 1, 1, 2, 3, 5, 8, 13, 21, 34], $result, "Result is incorrect.");
    }

    public function testGetCustomerBalances()
    {
        $codingFun = new CodingFun();

        $customerTransactions = [
            'bill=9898',
            'bob=772',
            'james=2672',
            'jim=9872',
            'luke=2665',
            'jim=10000'
        ];

        $result = $codingFun->getCustomerBalances($customerTransactions);

        $this->assertEquals(
            [
                'bill' => 9898,
                'bob' => 772,
                'james' => 2672,
                'jim' => 19872,
                'luke' => 2665
            ],
            $result,
            'Result is incorrect.'
        );
    }

    public function testGetLotteryWinningsForSoleWinner()
    {
        $codingFun = new CodingFun();

        $totalWinnings = 212000;
        $winningNumber = 123777;
        $players = [
            'matt' => 187658,
            'kate' => 898722,
            'andy' => 772782,
            'jeremy' => 882992,
            'christian' => 900220,
            'eva' => 123777,
            'jenny' => 989772,
            'corrie' => 123456
        ];

        $result = $codingFun->getLotteryWinnings($totalWinnings, $winningNumber, $players);

        $this->assertEquals(['eva' => 212000], $result, 'Result is incorrect.');
    }

    public function testGetLotteryWinningsForMultipleWinners()
    {
        $codingFun = new CodingFun();

        $totalWinnings = 2557767;
        $winningNumber = 223344;
        $players = [
            'matt' => 187658,
            'kate' => 898722,
            'andy' => 223344,
            'jeremy' => 223344,
            'christian' => 900220,
            'eva' => 123777,
            'jenny' => 223344,
            'corrie' => 123456
        ];

        $result = $codingFun->getLotteryWinnings($totalWinnings, $winningNumber, $players);

        $this->assertEquals(
            [
                'andy' => 852589,
                'jenny' => 852589,
                'jeremy' => 852589
            ],
            $result,
            'Result is incorrect.'
        );
    }
}

Source: Ask PHP

LEAVE A COMMENT