710145. Geek fight

Medium

Geek Fight

Summary

In this problem, we are given a list of geeks (represented as strings) and their corresponding ages. Our task is to find the maximum age among all geeks and return the name of the oldest geek along with his age.

The key concepts involved in this problem are sorting and comparison between strings.

Detailed Explanation

To solve this problem, we can use a simple sorting algorithm like bubble sort or selection sort. We will iterate through the list of geeks and compare their ages. If an older geek is found, we update our result with his name and age.

Here's a step-by-step breakdown of the solution:

  1. Initialize the maximum age to 0 and the oldest geek's name to an empty string.
  2. Iterate through the list of geeks.
  3. For each geek, compare their age with the current maximum age.
  4. If the geek's age is greater than the current maximum age, update the maximum age and the oldest geek's name.
  5. Return the name of the oldest geek along with his age.

Here's an ASCII art diagram to illustrate the process:

  +---------------+
  | Geek1: John (25) |
  +---------------+
  | Geek2: Alice (30) |
  +---------------+
  | Geek3: Bob (20)   |
  +---------------+
  ...
  | GeekN: Emma (40)  |
  +---------------+

  Iterate through the list:
  +---------------+
  | Geek1: John (25) |
  +---------------+
    -> Age < MaxAge, skip
  | Geek2: Alice (30) |
  +---------------+
    -> Age > MaxAge, update MaxAge and OldestGeekName
  ...
  | GeekN: Emma (40)  |
  +---------------+
    -> Age > MaxAge, update MaxAge and OldestGeekName

  Return the result:
  +---------------+
  | OldestGeekName: Alice (30) |
  +---------------+

Time complexity: O(n), where n is the number of geeks. Space complexity: O(1), as we only use a constant amount of space to store the maximum age and the oldest geek's name.

Optimized Solutions

Java

View on GeeksforGeeks
public class GeekFight {
    public static String findOldestGeek(String[] geeks) {
        int maxAge = 0;
        String oldestGeekName = "";
        for (String geek : geeks) {
            String[] parts = geek.split(": ");
            int age = Integer.parseInt(parts[1].trim());
            if (age > maxAge) {
                maxAge = age;
                oldestGeekName = parts[0];
            }
        }
        return oldestGeekName + " (" + maxAge + ")";
    }
}

DevExCode

Master coding interviews with Leetcode, system design, TechBit, QuickLearn, DevTips, Tech Battles, and career services.

© 2026 DevExCode. All rights reserved.