Bird
Raised Fist0
Prompt Engineering / GenAIml~20 mins

Chains (sequential, router) in Prompt Engineering / GenAI - Practice Problems & Coding Challenges

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Challenge - 5 Problems
🎖️
Chain Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
🧠 Conceptual
intermediate
1:30remaining
Understanding Sequential Chains

In a sequential chain, multiple steps are executed one after another. Which statement best describes how data flows through a sequential chain?

AAll steps run independently and their outputs are combined at the end.
BEach step receives input from the previous step's output and passes its output to the next step.
CThe first step receives all inputs and the last step produces all outputs without intermediate processing.
DSteps randomly select inputs from the original data without following order.
Attempts:
2 left
💡 Hint

Think about a relay race where one runner passes the baton to the next.

Model Choice
intermediate
1:30remaining
Choosing a Router Chain Use Case

You want to build a system that decides which specialized model to use based on the input type (e.g., text, image, or audio). Which chain type is best suited for this task?

ASequential chain that processes all inputs through every model in order.
BParallel chain that runs all models simultaneously and averages their outputs.
CRouter chain that selects the appropriate model based on input characteristics.
DSingle model chain that handles all input types without selection.
Attempts:
2 left
💡 Hint

Consider a traffic controller directing cars to different lanes based on their type.

Predict Output
advanced
2:00remaining
Output of a Sequential Chain Code

What is the output of the following pseudo-code for a sequential chain?

Prompt Engineering / GenAI
def step1(x):
    return x + 2

def step2(y):
    return y * 3

input_value = 4
output = step2(step1(input_value))
print(output)
A20
B14
C12
D18
Attempts:
2 left
💡 Hint

Calculate step1 first, then use its output in step2.

Metrics
advanced
2:00remaining
Evaluating Router Chain Performance

You have a router chain that directs inputs to three models. After testing, you get these accuracies: Model A: 90%, Model B: 85%, Model C: 80%. The router correctly routes 95% of inputs. What is the approximate overall accuracy of the router chain?

A88.25%
B92.50%
C90.00%
D85.75%
Attempts:
2 left
💡 Hint

Calculate weighted accuracy considering routing correctness and model accuracies.

🔧 Debug
expert
2:30remaining
Debugging a Router Chain Routing Error

Given this router chain pseudo-code, what causes the routing error?

def router(input):
    if 'image' in input:
        return 'model_image'
    elif 'text' in input:
        return 'model_text'
    else:
        return 'model_default'

input_data = 'audio file'
selected_model = router(input_data)
print(selected_model)
AThe router does not handle 'audio' inputs, so it returns 'model_default' which may not exist.
BThe condition 'if "image" in input' causes a syntax error due to missing colon.
CThe input_data variable is not defined before calling router.
DThe router function returns a list instead of a string, causing a type error.
Attempts:
2 left
💡 Hint

Check how the router handles inputs not matching 'image' or 'text'.

Practice

(1/5)
1. What is the main purpose of a sequential chain in GenAI?
easy
A. To run all AI steps at the same time
B. To randomly select one AI step to run
C. To run multiple AI steps one after another in order
D. To stop the AI process after the first step

Solution

  1. Step 1: Understand sequential chain behavior

    A sequential chain connects AI steps so they run one after another, passing output from one to the next.
  2. Step 2: Compare options to definition

    Only To run multiple AI steps one after another in order describes running steps in order, matching the sequential chain purpose.
  3. Final Answer:

    To run multiple AI steps one after another in order -> Option C
  4. Quick Check:

    Sequential chain = run steps in order [OK]
Hint: Sequential means steps run one after another [OK]
Common Mistakes:
  • Thinking sequential means random step selection
  • Confusing sequential with parallel execution
  • Assuming sequential chains stop early
2. Which of the following is the correct way to define a router chain in GenAI?
easy
A. router = SequentialChain(steps=[step1, step2])
B. router = RouterChain(steps=[step1, step2], router_function=choose_step)
C. router = RouterChain(steps=step1, step2)
D. router = ChainRouter(steps=[step1, step2])

Solution

  1. Step 1: Recall router chain syntax

    A router chain requires a list of steps and a router function to decide which step to run.
  2. Step 2: Check each option's syntax

    router = RouterChain(steps=[step1, step2], router_function=choose_step) correctly uses RouterChain with steps list and router_function parameter. Others have wrong class names or syntax.
  3. Final Answer:

    router = RouterChain(steps=[step1, step2], router_function=choose_step) -> Option B
  4. Quick Check:

    RouterChain needs steps list and router_function [OK]
Hint: RouterChain needs steps list and router_function param [OK]
Common Mistakes:
  • Using SequentialChain instead of RouterChain
  • Passing steps without brackets as list
  • Using wrong class names like ChainRouter
3. Given the code below, what will be the output?
def router_func(input_text):
    if 'weather' in input_text.lower():
        return 'weather_step'
    else:
        return 'default_step'

steps = {
    'weather_step': lambda x: 'It is sunny',
    'default_step': lambda x: 'I do not understand'
}

router_chain = RouterChain(steps=steps, router_function=router_func)

result = router_chain.run('What is the weather today?')
medium
A. 'It is sunny'
B. 'I do not understand'
C. Error: router_function missing
D. 'What is the weather today?'

Solution

  1. Step 1: Analyze router function behavior

    The router_func checks if 'weather' is in the input text (case-insensitive). Input contains 'weather', so it returns 'weather_step'.
  2. Step 2: Determine which step runs

    The router_chain uses 'weather_step' key to run the lambda returning 'It is sunny'.
  3. Final Answer:

    'It is sunny' -> Option A
  4. Quick Check:

    Input contains 'weather' -> weather_step -> 'It is sunny' [OK]
Hint: Router picks step by input keyword match [OK]
Common Mistakes:
  • Ignoring case in input text check
  • Confusing step keys with output strings
  • Assuming default_step runs always
4. Identify the error in this router chain code snippet:
steps = {
    'step1': lambda x: 'Hello',
    'step2': lambda x: 'Bye'
}

def router_func(input_text):
    if 'hello' in input_text:
        return 'step1'
    else:
        return 'step3'

router_chain = RouterChain(steps=steps, router_function=router_func)
result = router_chain.run('hello there')
medium
A. Lambda functions require two arguments
B. steps dictionary keys are not strings
C. router_function is missing in RouterChain
D. router_func returns a step key not in steps dictionary

Solution

  1. Step 1: Check router_func return values

    router_func returns 'step1' if 'hello' in input, else 'step3'. Input contains 'hello', so returns 'step1'.
  2. Step 2: Verify steps dictionary keys

    Steps dictionary has keys 'step1' and 'step2', but no 'step3'. Returning 'step3' would cause error if input changed.
  3. Final Answer:

    router_func returns a step key not in steps dictionary -> Option D
  4. Quick Check:

    Router returns unknown step key 'step3' [OK]
Hint: Router must return keys present in steps dict [OK]
Common Mistakes:
  • Ignoring missing step keys in router return
  • Assuming lambda needs multiple args
  • Forgetting to pass router_function parameter
5. You want to build a GenAI system that first summarizes a text, then translates it to French, but only if the text is longer than 100 words. Which chain setup is best?
hard
A. Use a sequential chain with a router function that skips translation if text is short
B. Use a router chain that chooses between summarization or translation only
C. Use two separate sequential chains running independently
D. Use a sequential chain that always runs summarization then translation

Solution

  1. Step 1: Understand task requirements

    The system must summarize first, then translate only if text is longer than 100 words.
  2. Step 2: Choose chain type matching conditional flow

    A sequential chain with a router function can run summarization step first, then decide to run translation step based on text length.
  3. Step 3: Evaluate other options

    Router chain alone can't enforce sequential order; two separate chains won't coordinate; always running translation ignores condition.
  4. Final Answer:

    Use a sequential chain with a router function that skips translation if text is short -> Option A
  5. Quick Check:

    Sequential + router for conditional step flow [OK]
Hint: Combine sequential steps with router for conditional logic [OK]
Common Mistakes:
  • Using router chain alone without sequence
  • Running translation always ignoring condition
  • Splitting steps into independent chains