Implement inner, left, right and full outer joins on R data frames with a fixed column and row order

Quick Overview

Implement inner, left, right and full outer joins for R data frames on one or more key columns, returning base data frames with a fixed column order, sorted rows and reset row names. It tests merge() semantics, NA filling, multi-key sorting and join edge cases such as duplicate and missing keys.

Implement inner, left, right and full outer joins on R data frames with a fixed column and row order

Role: Data Scientist

Category: Data Manipulation (SQL/Python)

Difficulty: medium

Interview Round: Other

Implement four join functions in `/app/solution.R`. Each takes `left`, `right` and `by` (a character vector of one or more key columns) and returns a base `data.frame`, not a tibble. - `inner_join_df(left, right, by)` keeps only key values present on both sides. - `left_join_df(left, right, by)` keeps every row of `left`, and `right_join_df(left, right, by)` keeps every row of `right`. Where a row has no match, the other side's columns are `NA`. - `outer_join_df(left, right, by)` keeps key values from either side, with missing columns filled with `NA`. Every result has the same shape. Columns come in this order: the keys in `by` order, then the non-key columns of `left`, then the non-key columns of `right`. Rows are sorted ascending by the keys in `by` order, and row names are reset to `1..n`. You may call `merge()` or write the join by hand. Non-key column names never clash between `left` and `right`. For example: ```r left <- data.frame(id = c(1, 2, 3), x = c("a", "b", "c")) right <- data.frame(id = c(2, 3, 4), y = c("p", "q", "r")) inner_join_df(left, right, by = "id") # id x y # 1 2 b p # 2 3 c q left_join_df(left, right, by = "id") # id x y # 1 1 a <NA> # 2 2 b p # 3 3 c q right_join_df(left, right, by = "id") # id x y # 1 2 b p # 2 3 c q # 3 4 <NA> r outer_join_df(left, right, by = "id") # id x y # 1 1 a <NA> # 2 2 b p # 3 3 c q # 4 4 <NA> r ``` ```hint One operation, four flags The four joins differ only in which unmatched rows they keep. Look for a way to write the matching once and switch the kept rows with arguments. ``` ```hint Normalize the output Whatever performs the match may return columns and rows in its own order. Plan a final step that fixes the column order, the row order and the row names for every join. ``` ### Constraints & Assumptions - `by` names one or more columns that exist in both `left` and `right`. - Non-key column names are distinct between the two inputs, so no suffixes are needed. - The return value must be a base `data.frame` with row names `1..n`, even when an input is a tibble. ### Clarifying Questions to Ask - Can a key value repeat on one side or on both sides, and should repeats produce every matching combination? - Should rows whose key is `NA` match each other, as they do in `merge()`, or never match, as in SQL? - Is any package allowed, or must the solution use base R only? ### What a Strong Answer Covers - How each of the four joins decides which unmatched rows to keep and fills the missing side with `NA`. - How the output columns are put into the required order for single and multiple keys. - How the rows are sorted by several keys at once, and how the row names are reset. - How the result is guaranteed to be a base `data.frame` whatever class the inputs have. - The edge cases: empty inputs, duplicate keys and `NA` keys. ### Follow-up Questions - How would you handle non-key column names that appear on both sides? - What changes for a many-to-many join, and how would you guard against unexpected row growth? - How would you implement the inner join by hand without `merge()`?

Overview: Implement inner, left, right and full outer joins for R data frames on one or more key columns, returning base data frames with a fixed column order, sorted rows and reset row names. It tests merge() semantics, NA filling, multi-key sorting and join edge cases such as duplicate and missing keys.

General logo
General
Nov 25, 2026
mediumData ScientistOtherData Manipulation (SQL/Python)
0
0

Implement four join functions in /app/solution.R. Each takes left, right and by (a character vector of one or more key columns) and returns a base data.frame, not a tibble.

  • inner_join_df(left, right, by) keeps only key values present on both sides.
  • left_join_df(left, right, by) keeps every row of left , and right_join_df(left, right, by) keeps every row of right . Where a row has no match, the other side's columns are NA .
  • outer_join_df(left, right, by) keeps key values from either side, with missing columns filled with NA .

Every result has the same shape. Columns come in this order: the keys in by order, then the non-key columns of left, then the non-key columns of right. Rows are sorted ascending by the keys in by order, and row names are reset to 1..n.

You may call merge() or write the join by hand. Non-key column names never clash between left and right.

For example:

left  <- data.frame(id = c(1, 2, 3), x = c("a", "b", "c"))
right <- data.frame(id = c(2, 3, 4), y = c("p", "q", "r"))

inner_join_df(left, right, by = "id")
#   id x y
# 1  2 b p
# 2  3 c q

left_join_df(left, right, by = "id")
#   id x    y
# 1  1 a <NA>
# 2  2 b    p
# 3  3 c    q

right_join_df(left, right, by = "id")
#   id    x y
# 1  2    b p
# 2  3    c q
# 3  4 <NA> r

outer_join_df(left, right, by = "id")
#   id    x    y
# 1  1    a <NA>
# 2  2    b    p
# 3  3    c    q
# 4  4 <NA>    r

Constraints & Assumptions

  • by names one or more columns that exist in both left and right .
  • Non-key column names are distinct between the two inputs, so no suffixes are needed.
  • The return value must be a base data.frame with row names 1..n , even when an input is a tibble.

Clarifying Questions to Ask Guidance

  • Can a key value repeat on one side or on both sides, and should repeats produce every matching combination?
  • Should rows whose key is NA match each other, as they do in merge() , or never match, as in SQL?
  • Is any package allowed, or must the solution use base R only?

What a Strong Answer Covers Guidance

  • How each of the four joins decides which unmatched rows to keep and fills the missing side with NA .
  • How the output columns are put into the required order for single and multiple keys.
  • How the rows are sorted by several keys at once, and how the row names are reset.
  • How the result is guaranteed to be a base data.frame whatever class the inputs have.
  • The edge cases: empty inputs, duplicate keys and NA keys.

Follow-up Questions Guidance

  • How would you handle non-key column names that appear on both sides?
  • What changes for a many-to-many join, and how would you guard against unexpected row growth?
  • How would you implement the inner join by hand without merge() ?