외국 코딩테스트 사이트 비추

yeahzxnn·2026년 2월 4일

CodingTest

목록 보기
12/16
post-thumbnail

데이터 분석가 쪽으로 확실히 분야를 잡아서
이 쪽으로 준비중입니다.

새로운 사이트들 도전 중인데 오늘은 아래 사이트를 도전해봤습니다.
근데 추천드리지 않습니다.

일단 Stratascratch는 hard를 풀려면 구독해야합니다...
문제 퀄리티는 좋아보여요 왜냐면 ebay, amazon 등 내노라하는 대기업 코드들이라서...
leetcode는 워낙 유명하지만 파이썬 기본 지식 있으시면 그냥 다른 거 푸십쇼...저는 파이썬 알고리즘에만 익숙하고, 파이썬을 SQL처럼 푸는 게 익숙하지가 않아서 기초부터 하려고 했는데 재미도 없고 뭘 배우는 지도 모르겠습니다.

내일부터는 다시 원래 하던 걸로 돌아오든 지 아니면 SQL만 일단 잡아도 될 것 같습니다. 보통 파이썬으로 시각화를 하지 저렇게 잘 쓰이는 것 같지는 않아서여..시간 낭비인듯

내일부터는 SQL만 더 연습하겠습니다.

그래도 오늘 푼 코드가 궁금하시다면 아래에서 보실 수 있습니다.


SQL

1.Recommendation System(Medium)

문제
You are given the list of Facebook friends and the list of Facebook pages that users follow. Your task is to create a new recommendation system for Facebook. For each Facebook user, find pages that this user doesn't follow but at least one of their friends does. Output the user ID and the ID of the page that should be recommended to this user.

핵심
친구 중 누군가가 팔로우하는 페이지는 추천
user → friend → friend가 팔로우한 page

SELECT DISTINCT
       uf.user_id,
       up.page_id
FROM users_friends uf
JOIN users_pages up
  ON uf.friend_id = up.user_id
WHERE NOT EXISTS (
    SELECT 1
    FROM users_pages my
    WHERE my.user_id = uf.user_id
      AND my.page_id = up.page_id
)

2.Customer Tracking(Hard)

Given the users' sessions logs on a particular day, calculate how many hours each user was active that day.

Note: The session starts when state=1 and ends when state=0.

핵심
윈도우 함수(Window Function)사용
핵심은 state=1(시작)일 때의 타임스탬프와 바로 다음에 오는 state=0(종료)일 때의 타임스탬프를 한 행에 모으기

SELECT 
    cust_id,
    SUM(TIMESTAMPDIFF(SECOND, timestamp, next_timestamp)) / 3600 AS hour_diff
FROM (
    SELECT 
        cust_id, 
        timestamp, 
        state,
        LEAD(timestamp) OVER (PARTITION BY cust_id ORDER BY timestamp) AS next_timestamp
    FROM cust_tracking
) t
WHERE state = 1 
GROUP BY cust_id;

Stratascratch 계획 곧 수정하게 될듯,,,무료로 풀려 있는 문제가 별로 없음...
무료로 있는 거라도 다 우선 풀게 되면 다른 거로 갈아탈게여


Python

LeetCode로 연습합니다.

595. Big Countries

문제

+-------------+---------+
| Column Name | Type |
+-------------+---------+
| name | varchar |
| continent | varchar |
| area | int |
| population | int |
| gdp | bigint |
+-------------+---------+
name is the primary key (column with unique values) for this table.
Each row of this table gives information about the name of a country, the continent to which it belongs, its area, the population, and its GDP value.

A country is big if:

it has an area of at least three million (i.e., 3000000 km2), or
it has a population of at least twenty-five million (i.e., 25000000).
Write a solution to find the name, population, and area of the big countries.

Return the result table in any order.

The result format is in the following example.

Example 1:

Input:
World table:
+-------------+-----------+---------+------------+--------------+
| name | continent | area | population | gdp |
+-------------+-----------+---------+------------+--------------+
| Afghanistan | Asia | 652230 | 25500100 | 20343000000 |
| Albania | Europe | 28748 | 2831741 | 12960000000 |
| Algeria | Africa | 2381741 | 37100000 | 188681000000 |
| Andorra | Europe | 468 | 78115 | 3712000000 |
| Angola | Africa | 1246700 | 20609294 | 100990000000 |
+-------------+-----------+---------+------------+--------------+
Output:
+-------------+------------+---------+
| name | population | area |
+-------------+------------+---------+
| Afghanistan | 25500100 | 652230 |
| Algeria | 37100000 | 2381741 |
+-------------+------------+---------+

핵심
SQL적으로 사고 하고 문법만 익히면 풀 수 있음
보통 시각화용도로만 사용했어서 이렇게 알고리즘처럼(?)
풀어본 적 없는데 신기
약간 더 익숙해져야할듯

import pandas as pd

def big_countries(world: pd.DataFrame) -> pd.DataFrame:
    condition = (world['area'] >= 3000000) | (world['population'] >= 25000000)
    return world[condition][['name', 'population', 'area']]

1757. Recyclable and Low Fat Products

문제
Table: Products

+-------------+---------+
| Column Name | Type |
+-------------+---------+
| product_id | int |
| low_fats | enum |
| recyclable | enum |
+-------------+---------+
product_id is the primary key (column with unique values) for this table.
low_fats is an ENUM (category) of type ('Y', 'N') where 'Y' means this product is low fat and 'N' means it is not.
recyclable is an ENUM (category) of types ('Y', 'N') where 'Y' means this product is recyclable and 'N' means it is not.

Write a solution to find the ids of products that are both low fat and recyclable.

Return the result table in any order.

The result format is in the following example.

Example 1:

Input:
Products table:
+-------------+----------+------------+
| product_id | low_fats | recyclable |
+-------------+----------+------------+
| 0 | Y | N |
| 1 | Y | Y |
| 2 | N | Y |
| 3 | Y | Y |
| 4 | N | N |
+-------------+----------+------------+
Output:
+-------------+
| product_id |
+-------------+
| 1 |
| 3 |
+-------------+
Explanation: Only products 1 and 3 are both low fat and recyclable.

핵심

import pandas as pd

def find_products(products: pd.DataFrame) -> pd.DataFrame:
    condition = (products['low_fats'] == 'Y') & (products['recyclable'] == 'Y')
    result = products[condition][['product_id']]
    
    return result
   

흠 오늘 코테 준비는 망한 것 같습니다...
사이트 적응만 하다가 시간 날린 것 같네요...

그냥 무료 사이트 hackerRank나 Leetcode로 계속 연습하는 게 나은 것 같습니다. 내일은 다시 원래 하던 대로 돌아올게요...

profile
Challenging & Growing

0개의 댓글