[Coding Question] “Second Highest Salary” – Dropbox

Coding Question - Second Highest Salary

Photo from engin akyurt on Unsplash

 

This is a rather typical coding question: finding the second maximum, or the third minimum and so on. Depending on the question, it may be the case that you need to only look among the unique values or just simply pick the n-th maximum/minimum from a list or a column. This coding question is from Dropbox and aims to find the second highest salary among a group of employees.

You can find the coding question here.

Here you will find the codes for the solution:

# Import your libraries
import pandas as pd

# Start writing code
df = employee

# Approach I
salaries = sorted(list(df.salary))
ans = salaries[-2]

# Approach II
df.sort_values(by = 'salary', ascending = False)['salary'].iloc[1]

# Approach III
df['salary'].nlargest(2).tail(1)

# Approach IV
df['salary_rank'] = df['salary'].rank(method = 'dense', ascending = False)
df[df.salary_rank == 2]['salary']

 

And here is the complete and step by step walkthrough and solution manual for this question:

 

 

 

 

 

Related Images: