Bird
Raised Fist0
Snowflakecloud~20 mins

DataFrame API in Snowpark in Snowflake - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Snowpark DataFrame Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ service_behavior
intermediate
2:00remaining
Understanding DataFrame Lazy Evaluation in Snowpark

Consider the following Snowpark code snippet:

df = session.table("EMPLOYEES").filter(col("SALARY") > 50000)
df.count()

What happens when df.count() is called?

Snowflake
df = session.table("EMPLOYEES").filter(col("SALARY") > 50000)
df.count()
AThe filter operation runs immediately and returns the count of employees with salary over 50000.
BNo query runs until an action like count() is called; count() triggers the query execution and returns the number of matching rows.
CThe count() method returns the total number of rows in the EMPLOYEES table ignoring the filter.
DThe filter operation runs immediately but count() only returns a DataFrame object without executing the query.
Attempts:
2 left
💡 Hint

Think about when Snowpark sends queries to Snowflake.

❓ Configuration
intermediate
2:00remaining
Correctly Creating a Snowpark DataFrame from a SQL Query

You want to create a DataFrame from a SQL query string in Snowpark. Which code snippet correctly creates the DataFrame?

Adf = session.sql("SELECT * FROM EMPLOYEES WHERE DEPARTMENT = 'SALES'")
Bdf = session.table("SELECT * FROM EMPLOYEES WHERE DEPARTMENT = 'SALES'")
Cdf = session.execute("SELECT * FROM EMPLOYEES WHERE DEPARTMENT = 'SALES'")
Ddf = session.query("SELECT * FROM EMPLOYEES WHERE DEPARTMENT = 'SALES'")
Attempts:
2 left
💡 Hint

Check which method accepts raw SQL strings.

❓ Architecture
advanced
2:00remaining
Optimizing Snowpark DataFrame Operations for Large Datasets

You have a large dataset and want to optimize your Snowpark DataFrame transformations to minimize data scanned and improve performance. Which approach is best?

AUse multiple collect() calls to fetch intermediate results to speed up processing.
BLoad the entire dataset into memory before applying any filters or transformations.
CApply filters early in the DataFrame chain to reduce data processed in later steps.
DAvoid using filters and rely on Snowflake's automatic optimization only.
Attempts:
2 left
💡 Hint

Think about reducing data volume early.

❓ security
advanced
2:00remaining
Handling Sensitive Data with Snowpark DataFrames

You need to process sensitive customer data using Snowpark DataFrames. Which practice ensures data security during processing?

AUse Snowflake's masking policies and restrict DataFrame access to authorized roles only.
BExport the data to local files, mask it there, then reload into Snowpark DataFrames.
CDisable all Snowflake security features and rely on Snowpark's client-side encryption.
DShare the DataFrame with all users to speed up processing and mask data manually.
Attempts:
2 left
💡 Hint

Consider built-in Snowflake security features.

✅ Best Practice
expert
2:00remaining
Ensuring Efficient Resource Usage with Snowpark Sessions

You have multiple Snowpark DataFrame operations running in parallel in your application. What is the best practice to manage Snowpark sessions to optimize resource usage?

AAvoid using Snowpark sessions and run raw SQL queries instead.
BCreate a new Snowpark session for each DataFrame operation to isolate workloads.
CClose the Snowpark session immediately after creating each DataFrame to free resources.
DCreate a single shared Snowpark session and reuse it across all operations to reduce overhead.
Attempts:
2 left
💡 Hint

Think about session overhead and resource management.

Practice

(1/5)
1. What does the filter() method do in Snowpark's DataFrame API?
easy
A. It adds new columns to the DataFrame.
B. It changes the column names of the DataFrame.
C. It selects rows that meet a specified condition.
D. It sorts the DataFrame by a column.

Solution

  1. Step 1: Understand the purpose of filter()

    The filter() method is used to keep only rows that satisfy a condition.
  2. Step 2: Compare with other methods

    Changing column names is done by withColumnRenamed(), adding columns by withColumn(), and sorting by orderBy().
  3. Final Answer:

    It selects rows that meet a specified condition. -> Option C
  4. Quick Check:

    filter() = select rows [OK]
Hint: Filter keeps rows matching condition, like a sieve [OK]
Common Mistakes:
  • Confusing filter() with select()
  • Thinking filter() changes columns
  • Using filter() to sort data
2. Which of the following is the correct way to select columns name and age from a DataFrame df in Snowpark?
easy
A. df.select('name', 'age')
B. df.filter('name', 'age')
C. df.orderBy('name', 'age')
D. df.withColumn('name', 'age')

Solution

  1. Step 1: Identify method to pick columns

    The select() method is used to choose specific columns from a DataFrame.
  2. Step 2: Check other methods

    filter() is for rows, orderBy() sorts rows, and withColumn() adds or modifies columns.
  3. Final Answer:

    df.select('name', 'age') -> Option A
  4. Quick Check:

    Select columns = select() [OK]
Hint: Use select() to pick columns, not filter() or orderBy() [OK]
Common Mistakes:
  • Using filter() to select columns
  • Trying to order columns instead of rows
  • Confusing withColumn() with select()
3. Given the following Snowpark code:
df = session.create_dataframe([(1, 'Alice'), (2, 'Bob'), (3, 'Carol')], schema=['id', 'name'])
df_filtered = df.filter(df['id'] > 1).select('name')
df_filtered.show()

What will be the output?
medium
A. Rows with names 'Bob' and 'Carol'
B. Rows with names 'Alice' only
C. Rows with ids 1, 2, and 3
D. Empty DataFrame

Solution

  1. Step 1: Understand the filter condition

    The filter keeps rows where id > 1, so rows with id 2 and 3 remain.
  2. Step 2: Select the 'name' column

    After filtering, only the 'name' column is selected, so output shows 'Bob' and 'Carol'.
  3. Final Answer:

    Rows with names 'Bob' and 'Carol' -> Option A
  4. Quick Check:

    Filter id>1 then select name = Bob, Carol [OK]
Hint: Filter first, then select columns to get desired rows [OK]
Common Mistakes:
  • Including 'Alice' who has id 1
  • Showing all columns instead of just 'name'
  • Expecting empty output
4. What is wrong with this Snowpark code snippet?
df = session.create_dataframe([(10, 'X'), (20, 'Y')], schema=['score', 'label'])
df_filtered = df.filter(df['score'] = 20)
df_filtered.show()
medium
A. Using show() instead of collect()
B. Missing parentheses around filter condition
C. Incorrect schema definition
D. Using single equals (=) instead of double equals (==) in filter condition

Solution

  1. Step 1: Check filter condition syntax

    The filter condition uses = which is assignment, not comparison. It should be ==.
  2. Step 2: Verify other parts

    Schema is correct, parentheses are fine, and show() is valid to display results.
  3. Final Answer:

    Using single equals (=) instead of double equals (==) in filter condition -> Option D
  4. Quick Check:

    Use == for comparison in filter [OK]
Hint: Use == for conditions, not = [OK]
Common Mistakes:
  • Using = instead of == in conditions
  • Thinking show() is invalid
  • Confusing schema errors with syntax errors
5. You want to create a new column age_group in a Snowpark DataFrame df that labels ages below 18 as 'minor' and 18 or above as 'adult'. Which code snippet correctly achieves this?
hard
A. df.select('age_group', F.when(df['age'] < 18, 'minor').otherwise('adult'))
B. df.withColumn('age_group', F.when(df['age'] < 18, 'minor').otherwise('adult'))
C. df.filter(df['age_group'] == 'minor')
D. df.orderBy('age_group')

Solution

  1. Step 1: Understand how to add a conditional column

    The withColumn() method adds or replaces a column. Using F.when().otherwise() sets values conditionally.
  2. Step 2: Check other options

    select() chooses columns but does not add new ones, filter() filters rows, and orderBy() sorts rows.
  3. Final Answer:

    df.withColumn('age_group', F.when(df['age'] < 18, 'minor').otherwise('adult')) -> Option B
  4. Quick Check:

    Add conditional column with withColumn + when [OK]
Hint: Use withColumn and when() to add conditional columns [OK]
Common Mistakes:
  • Using select() to add columns
  • Filtering instead of adding columns
  • Ordering instead of creating new columns
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard