Asked inWalmart,SDE-2

Maximum Length Pair Chain Problem Statement

You are provided with 'N' pairs of integers such that in any given pair (a, b), the first number is always smaller than the second number, i.e., a < b. A pair chain is defined as a sequential arrangement of pairs where a pair (c, d) can follow another pair (a, b) if b < c.

Objective:

Determine the length of the longest pair chain that can be formed using the given pairs.

Example:

Input:
Pairs =  [3,4], [1,2], [2,3]
Output:
2
Explanation:

The longest pair chain is [1,2] -> [3,4].

Constraints:

  • 1 <= T <= 100
  • 1 <= N <= 104
  • -109 <= a, b <= 109
Note:

- Each pair can be used only once.

- Not all pairs need to be used.

- Pairs may be selected in any order.

Input Format:

The first line contains an integer ‘T’ representing the number of test cases.
For each test case, the first line contains a single positive integer ‘N’ which is the number of pairs.
Each of the next ‘N’ lines contains two integers 'a' and 'b' denoting the pair elements.

Output Format:

For each test case, return a positive integer indicating the maximum possible length of the pair chain. Print each result on a new line.
Additional Note:
Implement the function only; do not handle input/output as it is managed elsewhere.
AnswerBot
4mo

Given pairs of integers, find the length of the longest pair chain where each pair follows the given condition.

  • Sort the pairs based on the second element in ascending order.

  • Iterate through the sorted ...read more

Help your peers!
Select
Add answer anonymously...

Walmart SDE-2 interview questions & answers

A SDE-2 was asked Q. Bipartite Graph Problem Statement Determine if a given graph is bipartite. A gra...read more
A SDE-2 was asked Q. Can you explain the DHCP Protocol?
A SDE-2 was asked Q. Can you explain the TCP/IP protocol?

Popular interview questions of SDE-2

A SDE-2 was asked Q1. Bipartite Graph Problem Statement Determine if a given graph is bipartite. A gra...read more
A SDE-2 was asked Q2. Can you explain the DHCP Protocol?
A SDE-2 was asked Q3. Can you explain the TCP/IP protocol?
Walmart SDE-2 Interview Questions
Stay ahead in your career. Get AmbitionBox app
play-icon
play-icon
qr-code
Trusted by over 1.5 Crore job seekers to find their right fit company
80 L+

Reviews

10L+

Interviews

4 Cr+

Salaries

1.5 Cr+

Users

Contribute to help millions

Made with ❤️ in India. Trademarks belong to their respective owners. All rights reserved © 2025 Info Edge (India) Ltd.

Follow Us
  • Youtube
  • Instagram
  • LinkedIn
  • Facebook
  • Twitter
Profile Image
Hello, Guest
AmbitionBox Employee Choice Awards 2025
Winners announced!
awards-icon
Contribute to help millions!
Write a review
Write a review
Share interview
Share interview
Contribute salary
Contribute salary
Add office photos
Add office photos
Add office benefits
Add office benefits