Bird
Raised Fist0
PHPprogramming~20 mins

DNF types (Disjunctive Normal Form) in PHP - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
DNF Mastery Badge
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
Output of PHP code with DNF type evaluation

What is the output of this PHP code that evaluates a simple DNF expression?

PHP
<?php
function evaluateDNF(array $clauses, array $values): bool {
    foreach ($clauses as $clause) {
        $clauseResult = true;
        foreach ($clause as $var => $negate) {
            $val = $values[$var] ?? false;
            if ($negate) {
                $val = !$val;
            }
            $clauseResult = $clauseResult && $val;
            if (!$clauseResult) {
                break;
            }
        }
        if ($clauseResult) {
            return true;
        }
    }
    return false;
}

$dnf = [
    ['A' => false, 'B' => true],
    ['C' => false]
];
$values = ['A' => true, 'B' => false, 'C' => true];

var_export(evaluateDNF($dnf, $values));
?>
Atrue
B1
Cnull
Dfalse
Attempts:
2 left
💡 Hint

Check each clause and see if any clause evaluates to true.

🧠 Conceptual
intermediate
1:30remaining
Understanding DNF clause structure in PHP arrays

In PHP, a DNF formula is represented as an array of clauses, where each clause is an associative array with variable names as keys and booleans indicating negation as values. Which of the following best describes the meaning of the boolean value in each clause?

ATrue means the variable is set to true, false means set to false
BTrue means the variable is included, false means it is excluded from the clause
CTrue means the variable is negated, false means it is not negated
DTrue means the variable is optional, false means mandatory
Attempts:
2 left
💡 Hint

Think about how negation is represented in boolean logic.

🔧 Debug
advanced
2:00remaining
Identify the error in DNF evaluation function

Given the following PHP function to evaluate a DNF formula, what error will occur when running it?

PHP
<?php
function evalDNF(array $dnf, array $vals): bool {
    foreach ($dnf as $clause) {
        $result = true;
        foreach ($clause as $var => $neg) {
            $val = $vals[$var];
            if ($neg) {
                $val = !$val;
            }
            $result = $result && $val;
        }
        if ($result) {
            return true;
        }
    }
    return false;
}

$dnf = [['X' => false]];
$vals = [];
evalDNF($dnf, $vals);
?>
AUndefined index notice for 'X'
BTypeError due to invalid argument
CReturns false without error
DSyntax error due to missing semicolon
Attempts:
2 left
💡 Hint

Check what happens when accessing an array key that does not exist.

📝 Syntax
advanced
1:30remaining
Which PHP code snippet correctly defines a DNF clause?

Which of the following PHP code snippets correctly defines a DNF clause representing (A OR NOT B)?

A$clause = ['A' => true, 'B' => false];
B$clause = ['A' => false, 'B' => true];
C$clause = ['A' => 1, 'B' => 0];
D$clause = ['A' => 'false', 'B' => 'true'];
Attempts:
2 left
💡 Hint

Recall that true means negated, false means not negated.

🚀 Application
expert
2:30remaining
Count how many clauses evaluate to true in a DNF formula

Given the PHP function below and the inputs, what is the number of clauses that evaluate to true?

PHP
<?php
function countTrueClauses(array $dnf, array $vals): int {
    $count = 0;
    foreach ($dnf as $clause) {
        $clauseTrue = true;
        foreach ($clause as $var => $neg) {
            $val = $vals[$var] ?? false;
            if ($neg) {
                $val = !$val;
            }
            if (!$val) {
                $clauseTrue = false;
                break;
            }
        }
        if ($clauseTrue) {
            $count++;
        }
    }
    return $count;
}

$dnf = [
    ['A' => false, 'B' => true],
    ['B' => false, 'C' => false],
    ['A' => true, 'C' => true]
];
$vals = ['A' => true, 'B' => false, 'C' => true];

echo countTrueClauses($dnf, $vals);
?>
A2
B1
C3
D0
Attempts:
2 left
💡 Hint

Evaluate each clause carefully with given values and negations.

Practice

(1/5)
1.

What does a Disjunctive Normal Form (DNF) type represent in PHP?

easy
A. A type that disables type checking
B. A type that combines OR (union) and AND (intersection) conditions
C. A type that restricts variables to integers only
D. A type that only allows a single data type

Solution

  1. Step 1: Understand DNF type concept

    DNF types combine multiple conditions using OR (union) and AND (intersection) to specify allowed types.
  2. Step 2: Compare options with definition

    Only the type that combines OR (union) and AND (intersection) conditions correctly describes this combination of OR and AND conditions in PHP types.
  3. Final Answer:

    A type that combines OR (union) and AND (intersection) conditions -> Option B
  4. Quick Check:

    DNF types = OR + AND conditions [OK]
Hint: DNF means combining OR and AND in types [OK]
Common Mistakes:
  • Thinking DNF only means union types
  • Confusing DNF with simple single types
  • Assuming DNF disables type checks
2.

Which of the following is the correct PHP syntax for a DNF type combining union and intersection?

function example( /* type here */ $param) {}
easy
A. int&float&Serializable
B. int&float|Serializable
C. int|float|Serializable
D. int|float&Serializable

Solution

  1. Step 1: Recall PHP DNF syntax rules

    PHP uses | for union (OR) and & for intersection (AND). Intersection has higher precedence, so int|float&Serializable means int OR (float AND Serializable).
  2. Step 2: Analyze each option

    int|float&Serializable correctly uses union and intersection combined. int&float|Serializable misplaces operators, C uses only union, D uses only intersection.
  3. Final Answer:

    int|float&Serializable -> Option D
  4. Quick Check:

    Union (|) and intersection (&) combined correctly = int|float&Serializable [OK]
Hint: Use | for OR, & for AND in PHP types [OK]
Common Mistakes:
  • Mixing order of | and & operators
  • Using only unions or only intersections
  • Writing invalid syntax like &| or |&
3.

What is the output of this PHP code snippet?

interface A {} 
interface B {} 
class C implements A, B {} 
function test(A|B&object $param): string { 
  return $param instanceof B ? 'Yes' : 'No'; 
} 
$obj = new C(); 
echo test($obj);
medium
A. Fatal error
B. No
C. Yes
D. Empty output

Solution

  1. Step 1: Understand the type declaration

    The parameter type is A OR (B AND object). Class C implements both A and B, so it matches the type.
  2. Step 2: Evaluate the instanceof check

    $obj is instance of B, so the ternary returns 'Yes'.
  3. Final Answer:

    Yes -> Option C
  4. Quick Check:

    Object matches B, so output is 'Yes' [OK]
Hint: Check instanceof for intersection parts in DNF types [OK]
Common Mistakes:
  • Assuming type mismatch causes error
  • Ignoring intersection precedence
  • Thinking output is 'No' without checking instanceof
4.

Identify the error in this PHP function using DNF types:

function checkType(int|float&Countable $value) { 
  return is_countable($value); 
}
medium
A. Invalid use of & with float, which is not an object
B. Missing return type declaration
C. is_countable() cannot be used here
D. int and float cannot be unioned

Solution

  1. Step 1: Analyze intersection with primitive types

    float is a primitive type and cannot be combined with & Countable (an interface) because & requires object types.
  2. Step 2: Confirm error cause

    Using & between float and Countable causes a type error since float is not an object.
  3. Final Answer:

    Invalid use of & with float, which is not an object -> Option A
  4. Quick Check:

    Intersection requires objects, float is primitive [OK]
Hint: Intersection (&) only works with object types [OK]
Common Mistakes:
  • Thinking primitives can be intersected with interfaces
  • Ignoring PHP type system rules
  • Assuming is_countable() causes error
5.

Given these interfaces and classes:

interface X {} 
interface Y {} 
class A implements X {} 
class B implements Y {} 
class C implements X, Y {}

Which DNF type correctly accepts objects that implement either X alone, or both X and Y together?

hard
A. X|(X&Y)
B. X&Y|Y
C. X|Y
D. (X|Y)&Y

Solution

  1. Step 1: Understand the requirement

    Accept objects that implement X alone OR both X and Y together.
  2. Step 2: Translate to DNF

    This means either X alone OR (X AND Y). X|(X&Y) expresses this as X|(X&Y).
  3. Step 3: Check other options

    X&Y|Y includes Y alone, which is incorrect. X|Y accepts X or Y alone, too broad. (X|Y)&Y includes Y alone, which is incorrect.
  4. Final Answer:

    X|(X&Y) -> Option A
  5. Quick Check:

    DNF matches X alone or X and Y together = X|(X&Y) [OK]
Hint: Use union of X and intersection X&Y for this case [OK]
Common Mistakes:
  • Choosing simple union X|Y which is too broad
  • Misplacing parentheses changing logic
  • Confusing intersection and union precedence
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard