PPractick

DSA interview question

Leaders in an Array

Easy · Algorithms · Topics: array

The problem

A marathon's live tracker lists the runners' current speeds from the front of the pack to the back, and marks a runner as a leader if nobody behind them is faster, meaning their speed is greater than or equal to every speed to their right. The last runner is always a leader. Checking every runner against everyone behind them is O(n²). Scanning from the right instead, keeping the maximum seen so far, identifies all leaders in one pass.

Example: [16, 17, 4, 3, 5, 2] → [17, 5, 2].

Function signature

vector<int> leaders(const vector<int>& a);  // leaders in their original left-to-right order

Test cases

Input aOutputExplanation
[16, 17, 4, 3, 5, 2][17, 5, 2]
[10, 4, 2, 4, 1][10, 4, 4, 1]Equal to the maximum on the right still counts
[5, 4, 3, 2, 1][5, 4, 3, 2, 1]Every element is a leader
[1, 2, 3, 4, 5][5]Only the last element
[7][7]
[1, 1, 1][1, 1, 1]

(Confirm: a leader is greater than or equal to every element to its right (the GeeksforGeeks definition); output in original order.)

How interviewers phrase it

Clarifying questions to ask

  1. Greater than, or greater than or equal to, the elements on the right?
  2. Should the output be in original order?
  3. Can values be negative?

Follow-ups to expect

  1. "Why scan from the right?"
  2. "How do you output leaders in the original order?"
  3. "What changes for a strict definition (greater than)?"
  4. "What's the brute force?"
  5. "Complexity?"

Practise questions like this with a real engineer

On Practick another engineer interviews you live for 60 minutes on a question from this bank. They have the full interviewer guide beside the call: the expected answer to every follow-up, hints to give if you stall, and a scoring rubric. You get the model solution once the session settles. It is free: interview someone for an hour and you have earned your own.

Book a free mock interview

Related questions

All 474 dsa interview questions

DSA mock interviews · System design interview questions · Free mock interview practice · Browse interviewers · Sign in