-2

I came across this php interview question while researching pre-interview test questions.

Given an array of integers, compute a total score based on the following rules:

  1. Add 1 point for every even number in the array
  2. Add 3 points for every odd number in the array
  3. Add 5 points for every time you encounter an 8 in the array

Examples:

  • Input: my_numbers = [1, 2, 3, 4, 5]

  • Output: 11

  • Input: my_numbers=[15, 25, 35]

  • Output: 9

  • Input: my_numbers=[8, 8]

  • Output: 10

How to approach this particular task? my attempt

    <?php function find_total( $my_numbers ) {
    //Insert your code here \
    $total = 0;
    foreach($my_numbers as $val) {
    if ($val % 2 == 0) {
    echo (++$val);
    } else 
    echo ($val += 3);
    }
    if ($val == 8) {
    echo($val += 5);}
    }
    ?>
  • Simple approach: 1. create a simple counter of zero. 2. Loop over each element and 3. check if it fits each rule. 4. If it does, increment the counter by the appropriate amount. 5. Return the counter. – waterloomatt Dec 16 '22 at 00:10
  • Is it possible for negative numbers to be present in the array? This is important to know, because an answer using the modulo operator `%` should account for the fact that a modulo on a negative number can produce negative values. – Robin Bastiaan Dec 16 '22 at 00:41
  • We'd like to see how far you got BEFORE you posted this (homework assignment / requirements dump) question. Are the rules exclusive? Can a single number not satisfy rule 1 and 3? Why is 8, 8 not 12? – mickmackusa Dec 16 '22 at 00:46
  • Functional approach for fun: [`echo array_sum(array_map(fn($v) => ($v & 1 ? 3 : 1) + ($v === 8 ? 4 : 0), $my_numbers));`](https://3v4l.org/pZF6i) – mickmackusa Dec 16 '22 at 01:04
  • the array containing -ve integers was not mentioned in the question – Ayomide Omotayo Dec 16 '22 at 01:15
  • Interview questions are less about achieving the result and more about exposing _your thought processes_. If you can complete the task with one approach that's great. If you can complete the task 6 different ways, how do you choose which way is best. Do you ask for the preferrences of the dev team? Do they have faux pas techniques? Are you aware enough to identify fringe cases not represented in the sample data? Do you know how to ask the right question to get sufficient clarification. These are the intangibles that interviewers are looking for. – mickmackusa Dec 16 '22 at 01:19
  • @Ayomide Omotayo, integers can be positive or negative. The OP said "an array of integers". Specifying that it can contain negative integers is no more necessary/unnecessary than specifying that it can contain positive integers, – Nikkorian Dec 16 '22 at 05:33

1 Answers1

1

I am not sure your sample answers are correct (8 is an even number and should attract 1 for that as well as the 5), but to give the answer you asked for use this (PHP):

(EDITED in response to mickmackusa's and Robin Bastiaan's comments)

$total = 0;
foreach($myarray as $value) {
    if ($value === 8) {
       $total += 5;
    } elseif (!($value % 2)) {
       ++$total;
    } else 
       $total += 3;
} 
Nikkorian
  • 770
  • 4
  • 10
  • Given the examples by the OP, I conclude that an 8 will only give 5 points even tho it is technically also an even number. Thus your solution is correct on that part. However, if the array can contain negative numbers, it will for example count the odd number of `-3` as value `1` while it should be `3` instead. – Robin Bastiaan Dec 16 '22 at 00:47
  • `$total += 1;` can be written as `++$total;` See also the bitwise odd check: [Odd and Even numbers (using & or %)](https://stackoverflow.com/q/17175947/2943403) – mickmackusa Dec 16 '22 at 00:57
  • @bastiaan i attached a screenshot of the question for better clarity – Ayomide Omotayo Dec 16 '22 at 01:16
  • @mickmackusa the screenshot is my attempt – Ayomide Omotayo Dec 16 '22 at 02:34
  • 1
    The earlier version of the screenshot (that I saw) did not contain any code in the function body. You should transfer your code to the question body as plain text because Stack Overflow does not prefer screenshots of text. @Ayo – mickmackusa Dec 16 '22 at 03:05