Solving FORBES Coding Interview Questions in PYTHON: 3 Simple Steps!

preview_player
Показать описание
This video will walk you through the Forbes Most Profitable Companies interview question and provide a Python solution. We'll show you three simple steps to help you solve this middle-level question. These steps will help you demonstrate your understanding of the question. After watching this video, you'll be able to solve this question easily.

Go to the question through the link below and follow along with me👇

______________________________________________________________________

______________________________________________________________________

Timeline:

Intro: (0:00​​​)
Interview Question: (0:22 )
Exploring and understanding the dataset: (0:55)
Writing out the approach: (2:53​​)
Coding the solution: (3:54)
Conclusion: (​7:03)

______________________________________________________________________

About The Platform:

______________________________________________________________________

Contact:

If you have any questions, comments, or feedback, please leave them here!
______________________________________________________________________
#StrataScratch #Python #PythonInterviewQuestions
Рекомендации по теме
Комментарии
Автор

Find matching hosts and guests in a way that they are both of the same gender and nationality

Airbnb
Easy
General Practice
ID 10078


import numpy
import pandas as pd

# step 1 :
# we have two tables merge them here.
# join them using merge() and common table is host_id.
# syntax :
# pd.merge(df1, df2, on='common column', how='type of join required.')
pd.merge(airbnb_hosts, airbnb_guests, on=['nationality', 'gender'], how='inner')

# step 2 :
# remove duplicates.
# syntax:
# .drop_duplicates()
pd.merge(airbnb_hosts, airbnb_guests, on=['nationality', 'gender'], how='inner').drop_duplicates()

# step 3 :
# Output the host id and the guest id of matched pair.
df=pd.merge(airbnb_hosts, airbnb_guests, on=['nationality', 'gender'], how='inner').drop_duplicates()
df
df[['host_id', 'guest_id']]

AIFashionistaGuide
Автор

Please give question solutions of premium questions

AIFashionistaGuide
Автор

Number Of Units Per Nationality


Airbnb
Easy
General Practice
ID 10156


import numpy
import pandas as pd

# step 1 :
# we have two tables merge them here.
# join them using merge() and common table is host_id.
# syntax :
# pd.merge(df1, df2, on='common column', how='type of join required.')
pd.merge(airbnb_hosts, airbnb_units, on='host_id', how='inner')

# step 2 :
# remove duplicates.
# syntax:
# .drop_duplicates()
pd.merge(airbnb_hosts, airbnb_units, on='host_id', how='inner').drop_duplicates()

# step 3:
# conditions applied using bullet indexing
# df[() & ()]
# & (df['age']<30)]
df=pd.merge(airbnb_hosts, airbnb_units, on='host_id', how='inner').drop_duplicates()
df
& (df['age']<30)]

# step 4:
# count the number of apartments based on natioanlity
# only one natinality present here...USA
# count() function is an aggreate function can be used only with groupby() function

# use groupby function
# &
# count() function on unit_id.
&

# Output the nationality along with the number of apartments.
# &
# gives error natiobality is not part of this dataframe output.

# both will work as_index and reset_index()
& (df['age']<30)].groupby('nationality', as_index=False)['unit_id'].count
&

AIFashionistaGuide
Автор

forbes_global_2010_2014['ranks']=forbes_global_2010_2014['profits'].rank(method='min', ascending=False)



I solved it with only 2 above steps

Soulfulreader
Автор

Ranking Most Active Guests


Airbnb
Medium
General Practice
ID 10159

import pandas as pd
import numpy as np

# step 1:
# get th total number of rows.
airbnb_contacts.info()
'''
<class
RangeIndex: 100 entries, 0 to 99
Data columns (total 11 columns):
'''
# step 2:
# get th total number of rows.
# use groupby and the sum of messages given here.
# gives only 'n_messages' column
df=
# gives only 'n_messages', 'id_guest' column
df=airbnb_contacts.groupby('id_guest', as_index=False)['n_messages'].sum()

# step 3:
# Order by the highest number of total messages first.
df=airbnb_contacts.groupby('id_guest', as_index=False)['n_messages'].sum()
df.sort_values('n_messages', ascending=False)
#
'''
id_guest n_messages


'''
# step 4:
# Order by the guest id also check the expected output.
'''
ranking id_guest n_messages


'''
df=airbnb_contacts.groupby('id_guest', as_index=False)['n_messages'].sum()
df1=df.sort_values(by=['n_messages', 'id_guest'], ascending=[False, True])

# step 5 :
# we have rank based on n_messages .
# using rank()
# syntax :
# dataframename['name of column name of rank']=dataframenaem.rank()
# rank() works on average
# signature : rank(axis=0, method='average', ...) check this in pop up.
# shows in decimal format.
df["Ranking"] = df['n_messages'].rank()

# step 6 :
# Guests with the same number of messages as other guests should have the same rank.
# Do not skip rankings if the preceding rankings are identical.
# using rank(method='dense')
df1["Ranking"] =
df1
# we want highest number of n_messages to be ranked 1
df1["Ranking"] = df1['n_messages'].rank(method='dense', ascending=False)
df1

# step 7 :
# Guests with the same number of messages as other guests should have the same rank.
# Do not skip rankings if the preceding rankings are identical.
# using rank(method='dense')
df1["Ranking"] =
df1
# we want highest number of n_messages to be ranked 1
df1["Ranking"] = df1['n_messages'].rank(method='dense', ascending=False)
df1

# step 8 :
# here raking column column_index[2] must at start
# df1.iloc[:, [0, 1, 2]]
df1.iloc[:, [2, 0, 1]]

AIFashionistaGuide