Advanced Coding Patterns for Interviews

0% completed

Previous
Next
Height Checker (easy)

Problem Statement

A school is organizing its annual photo session, and students must stand in a single line arranged by height in non-decreasing order. The expected order of heights is represented by an array expected, where expected[i] is the height of the i<sup>th</sup> student in line.

Given an array heights representing the current order in which students are standing, determine how many positions in the heights array do not match the expected order.

Examples

  1. Example 1:

    • Input: heights = [5, 1, 2, 3, 4, 8, 1]
    • Expected Output: 3
    • Justification: The expected order is [1, 1, 2, 3, 4, 5, 8]. Comparing with heights, the positions that do not match are 0, 5 and 6.
  2. Example 2:

    • Input: heights = [10, 6, 8, 5, 9]
    • Expected Output: 3
    • Justification: The expected order is [5, 6, 8, 9, 10]. Comparing with heights, the positions that do not match are 0, 3 and 4.
  3. Example 3:

    • Input: heights = [7, 3, 2, 1, 4]
    • Expected Output: 5
    • Justification: The expected order is [1, 2, 3, 4, 7]. All positions differ from heights.

Constraints:

  • 1 <= heights.length <= 100
  • 1 <= heights[i] <= 100

Solution

Try it yourself

Try solving this question here:

Python3
Python3

. . . .

.....

.....

.....

Like the course? Get enrolled and start learning!
Previous
Next