Bird
Raised Fist0
Hadoopdata~20 mins

User-defined functions (UDFs) in Hadoop - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
UDF Mastery Badge
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
Output of a simple UDF in Hive
Consider this Hive UDF written in Java. What will be the output when the UDF is applied to the input string "hadoop"?
Hadoop
public class ReverseStringUDF extends org.apache.hadoop.hive.ql.exec.UDF {
    public String evaluate(String input) {
        if (input == null) return null;
        return new StringBuilder(input).reverse().toString();
    }
}

-- Hive query:
SELECT ReverseStringUDF('hadoop');
A'hadoop'
B'poohad'
C'poodah'
Dnull
Attempts:
2 left
💡 Hint
Think about what the StringBuilder.reverse() method does to the input string.
❓ data_output
intermediate
2:00remaining
Result of applying a UDF to a Hive table column
Given a Hive table users with a column username, and a UDF ToUpperCaseUDF that converts strings to uppercase, what is the output of this query?
Hadoop
SELECT username, ToUpperCaseUDF(username) AS username_upper FROM users WHERE username = 'alice';
A[('alice', 'alice')]
B[]
C[('ALICE', 'ALICE')]
D[('alice', 'ALICE')]
Attempts:
2 left
💡 Hint
The UDF changes the case but does not filter rows.
🔧 Debug
advanced
2:00remaining
Identify the error in this UDF code
This Java UDF code is intended to return the length of a string. What error will it cause when compiled or run?
Hadoop
public class StringLengthUDF extends org.apache.hadoop.hive.ql.exec.UDF {
    public int evaluate(String input) {
        return input.length();
    }
}
ANullPointerException if input is null
BSyntaxError due to missing return type
CClassNotFoundException at runtime
DNo error, runs correctly
Attempts:
2 left
💡 Hint
What happens if the input string is null and you call length()?
🧠 Conceptual
advanced
2:00remaining
Understanding UDF registration in Hive
Which statement correctly describes how to register a Java UDF in Hive before using it in queries?
AUse <code>ADD JAR 'path_to_jar'; CREATE FUNCTION function_name AS 'class_name'</code>
BUse <code>REGISTER FUNCTION function_name AS 'class_name'</code>
CUse <code>CREATE FUNCTION function_name AS 'class_name' USING JAR 'path_to_jar'</code>
DNo registration needed; just call the UDF class directly in queries
Attempts:
2 left
💡 Hint
Think about how Hive loads external code and links it to a function name.
🚀 Application
expert
2:00remaining
Predict output of a complex UDF with multiple inputs
Given this UDF that concatenates two strings with a dash, what is the output of the Hive query?
Hadoop
public class ConcatDashUDF extends org.apache.hadoop.hive.ql.exec.UDF {
    public String evaluate(String s1, String s2) {
        if (s1 == null) s1 = "";
        if (s2 == null) s2 = "";
        return s1 + "-" + s2;
    }
}

-- Hive query:
SELECT ConcatDashUDF('data', NULL);
A'data-NULL'
B'data-'
C'-NULL'
Dnull
Attempts:
2 left
💡 Hint
Check how the UDF handles null inputs before concatenation.

Practice

(1/5)
1. What is the main purpose of a User-defined Function (UDF) in Hadoop?
easy
A. To add custom data processing logic not available in built-in functions
B. To store data in Hadoop Distributed File System (HDFS)
C. To manage cluster resources automatically
D. To monitor network traffic between nodes

Solution

  1. Step 1: Understand what UDFs do in Hadoop

    UDFs allow users to write their own functions to process data beyond what built-in functions offer.
  2. Step 2: Compare options with UDF purpose

    Only To add custom data processing logic not available in built-in functions describes adding custom data processing logic, which matches UDF use.
  3. Final Answer:

    To add custom data processing logic not available in built-in functions -> Option A
  4. Quick Check:

    UDF purpose = custom processing [OK]
Hint: UDFs add your own data steps in Hadoop [OK]
Common Mistakes:
  • Confusing UDFs with storage or resource management
  • Thinking UDFs handle system monitoring
  • Assuming UDFs replace built-in functions entirely
2. Which method must you implement when creating a Hadoop UDF class?
easy
A. initialize()
B. execute()
C. evaluate()
D. process()

Solution

  1. Step 1: Recall UDF class requirements

    Hadoop UDFs must extend the UDF class and implement the evaluate method to process input.
  2. Step 2: Match method names

    Only evaluate() is the required method for UDF logic; others are incorrect or do not exist.
  3. Final Answer:

    evaluate() -> Option C
  4. Quick Check:

    UDF method = evaluate() [OK]
Hint: UDFs always implement evaluate() method [OK]
Common Mistakes:
  • Using initialize() or execute() instead of evaluate()
  • Forgetting to override evaluate()
  • Confusing with other Hadoop APIs
3. Given this UDF code snippet, what will be the output when input is null?
public class MyUDF extends UDF {
  public String evaluate(String input) {
    if (input == null) return "empty";
    return input.toUpperCase();
  }
}
medium
A. null
B. "empty"
C. "EMPTY"
D. Runtime error

Solution

  1. Step 1: Analyze input null check in evaluate()

    The code checks if input is null and returns the string "empty" exactly as is.
  2. Step 2: Determine output for null input

    Since input is null, the method returns "empty" (lowercase), not uppercase or error.
  3. Final Answer:

    "empty" -> Option B
  4. Quick Check:

    null input returns "empty" [OK]
Hint: Check null input handling in evaluate() [OK]
Common Mistakes:
  • Assuming output is uppercase "EMPTY"
  • Expecting null to be returned
  • Thinking code throws an error on null
4. Identify the error in this UDF code:
public class SampleUDF extends UDF {
  public Integer evaluate(Integer num) {
    return num + 10;
  }
}
medium
A. Method name should be process, not evaluate
B. evaluate method must return String, not Integer
C. Class must implement interface, not extend UDF
D. Missing null check for input parameter

Solution

  1. Step 1: Review evaluate method logic

    The method adds 10 to input num but does not check if num is null, which can cause NullPointerException.
  2. Step 2: Validate other options

    Returning Integer is allowed; extending UDF is correct; method name evaluate is required.
  3. Final Answer:

    Missing null check for input parameter -> Option D
  4. Quick Check:

    Always check for null inputs in UDFs [OK]
Hint: Always check for null inputs in evaluate() [OK]
Common Mistakes:
  • Ignoring null input checks
  • Confusing return types for evaluate()
  • Misunderstanding class inheritance rules
5. You want to create a UDF that returns the length of a string, but returns 0 if input is null or empty. Which evaluate method implementation is correct?
public Integer evaluate(String input) {
  // Choose the correct code
}
hard
A. if (input == null || input.isEmpty()) return 0; else return input.length();
B. return input.length();
C. if (input == null) return null; else return input.length();
D. return input == null ? 0 : input.length() + 1;

Solution

  1. Step 1: Understand requirements for null or empty input

    The method must return 0 if input is null or empty string, otherwise return string length.
  2. Step 2: Evaluate each option

    if (input == null || input.isEmpty()) return 0; else return input.length(); correctly checks null or empty and returns 0; others either miss empty check or return wrong values.
  3. Final Answer:

    if (input == null || input.isEmpty()) return 0; else return input.length(); -> Option A
  4. Quick Check:

    Null or empty returns 0, else length [OK]
Hint: Check both null and empty before length [OK]
Common Mistakes:
  • Not checking for empty string
  • Returning null instead of 0
  • Adding extra 1 to length incorrectly
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