Find max in struct array

Last Updated : 2 May, 2026

Given a struct array of type Height, find max:

Question source : Microsoft Interview Experience Set 127 | (On-Campus for IDC) 

struct Height{
int feet;
int inches;
}

Try It Yourself
redirect icon

Convert each height into a single comparable unit (inches) and track the maximum while traversing the array once.

  • Initialize mx with minimum value
  • Traverse each element in the array. Convert height, total = feet * 12 + inches
  • Compare with current maximum and update mx if greater value is found
  • Return max
C++
#include <climits>
#include <iostream>
#include <vector>
using namespace std;

// struct Height
// 1 feet = 12 inches
struct Height {
    int feet;
    int inches;
};

// return max of the array
int findMax(vector<Height>& arr)
{
    int mx = INT_MIN;

    for (int i = 0; i < arr.size(); i++) {
        int temp = 12 * (arr[i].feet) + arr[i].inches;
        mx = max(mx, temp);
    }

    return mx;
}

// driver program
int main()
{
    // initialize the vector
    vector<Height> arr = {
        {1, 3}, {10, 5}, {6, 8}, {3, 7}, {5, 9}
    };

    int res = findMax(arr);

    cout << res << endl;

    return 0;
}
Java
import java.util.*;

// struct Height
// 1 feet = 12 inches
class Height {
    int feet;
    int inches;

    Height(int f, int i) {
        feet = f;
        inches = i;
    }
}

class GFG {

    // return max of the array
    static int findMax(ArrayList<Height> arr)
    {
        int mx = Integer.MIN_VALUE;

        for (int i = 0; i < arr.size(); i++) {
            int temp = 12 * arr.get(i).feet + arr.get(i).inches;
            mx = Math.max(mx, temp);
        }

        return mx;
    }

    public static void main(String[] args)
    {
        ArrayList<Height> arr = new ArrayList<>();

        arr.add(new Height(1, 3));
        arr.add(new Height(10, 5));
        arr.add(new Height(6, 8));
        arr.add(new Height(3, 7));
        arr.add(new Height(5, 9));

        System.out.println(findMax(arr));
    }
}
Python
# struct Height
# 1 feet = 12 inches
class Height:
    def __init__(self, feet, inches):
        self.feet = feet
        self.inches = inches

# return max of the array
def findMax(arr):
    mx = float('-inf')

    for i in range(len(arr)):
        temp = 12 * (arr[i].feet) + arr[i].inches
        mx = max(mx, temp)

    return mx


# driver program
arr = [
    Height(1, 3), Height(10, 5),
    Height(6, 8), Height(3, 7),
    Height(5, 9)
]

res = findMax(arr)
print(res)
C#
using System;
using System.Collections.Generic;

// struct Height
// 1 feet = 12 inches
class Height {
    public int feet;
    public int inches;

    public Height(int f, int i) {
        feet = f;
        inches = i;
    }
}

class GFG {

    // return max of the array
    static int findMax(List<Height> arr)
    {
        int mx = int.MinValue;

        for (int i = 0; i < arr.Count; i++) {
            int temp = 12 * arr[i].feet + arr[i].inches;
            mx = Math.Max(mx, temp);
        }

        return mx;
    }

    static void Main()
    {
        List<Height> arr = new List<Height>() {
            new Height(1, 3), new Height(10, 5),
            new Height(6, 8), new Height(3, 7),
            new Height(5, 9)
        };

        Console.WriteLine(findMax(arr));
    }
}
JavaScript
// struct Height
// 1 feet = 12 inches
class Height {
    constructor(feet, inches) {
        this.feet = feet;
        this.inches = inches;
    }
}

// return max of the array
function findMax(arr)
{
    let mx = Number.MIN_SAFE_INTEGER;

    for (let i = 0; i < arr.length; i++) {
        let temp = 12 * (arr[i].feet) + arr[i].inches;
        mx = Math.max(mx, temp);
    }

    return mx;
}

// driver program
let arr = [
    new Height(1, 3), new Height(10, 5),
    new Height(6, 8), new Height(3, 7),
    new Height(5, 9)
];

let res = findMax(arr);
console.log(res);

Output
125

Comment