Bird
Raised Fist0
NLPml~20 mins

Answer span extraction in NLP - 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
🎖️
Answer Span Extraction Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
🧠 Conceptual
intermediate
1:30remaining
What is the main goal of answer span extraction in NLP?

In simple terms, what does answer span extraction try to do when given a question and a paragraph?

AFind the exact part of the paragraph that answers the question
BGenerate a new answer unrelated to the paragraph
CSummarize the entire paragraph without focusing on the question
DTranslate the paragraph into another language
Attempts:
2 left
💡 Hint

Think about pointing to a specific piece of text that answers the question.

Predict Output
intermediate
2:00remaining
What is the output of this answer span extraction code snippet?

Given the paragraph and question, what answer span does the model predict?

NLP
paragraph = 'The Eiffel Tower is located in Paris and is one of the most famous landmarks.'
question = 'Where is the Eiffel Tower located?'

# Simulated model output (start and end indices)
start_index = 5
end_index = 6

answer_tokens = paragraph.split()[start_index:end_index+1]
answer = ' '.join(answer_tokens)
print(answer)
Ain Paris
Blocated in Paris
CParis and is
DThe Eiffel Tower
Attempts:
2 left
💡 Hint

Look at the words between indices 5 and 6 in the paragraph split by spaces.

Model Choice
advanced
2:00remaining
Which model architecture is best suited for answer span extraction tasks?

Choose the model type that is designed to predict start and end positions of answers in a paragraph.

ASequence-to-sequence model generating answers word by word
BTransformer-based model with token classification heads for start and end positions
CUnsupervised clustering model grouping similar sentences
DGenerative adversarial network creating new paragraphs
Attempts:
2 left
💡 Hint

Think about models that output positions rather than generating text.

Metrics
advanced
1:30remaining
Which metric best evaluates answer span extraction accuracy?

When checking if the predicted answer span matches the true answer span, which metric is most appropriate?

ABLEU score measuring n-gram overlap
BMean Squared Error between token embeddings
CExact Match (EM) score checking exact span equality
DPerplexity measuring language model uncertainty
Attempts:
2 left
💡 Hint

Think about a metric that checks if the predicted answer is exactly the same as the true answer.

🔧 Debug
expert
2:00remaining
Why does this answer span extraction code produce an empty answer?

Consider this code snippet that tries to extract an answer span but returns an empty string. What is the most likely cause?

NLP
paragraph = 'Machine learning helps computers learn from data.'
start_index = 7
end_index = 5

answer_tokens = paragraph.split()[start_index:end_index+1]
answer = ' '.join(answer_tokens)
print(answer)
AThe split method is called incorrectly without parentheses
BThe paragraph string is empty, so no tokens exist
CThe join method is used on a string instead of a list
Dstart_index is greater than end_index, so the slice is empty
Attempts:
2 left
💡 Hint

Check the order of start and end indices in the slice.

Practice

(1/5)
1. What is the main goal of answer span extraction in NLP?
easy
A. To generate new text based on a prompt
B. To find the exact part of text that answers a question
C. To summarize long documents into short sentences
D. To translate text from one language to another

Solution

  1. Step 1: Understand the purpose of answer span extraction

    Answer span extraction focuses on locating the exact segment in a text that directly answers a question.
  2. Step 2: Compare with other NLP tasks

    Unlike translation, summarization, or text generation, answer span extraction pinpoints a specific text span as the answer.
  3. Final Answer:

    To find the exact part of text that answers a question -> Option B
  4. Quick Check:

    Answer span extraction = find exact answer span [OK]
Hint: Answer span extraction locates exact text answers [OK]
Common Mistakes:
  • Confusing answer span extraction with translation
  • Thinking it summarizes text instead of extracting spans
  • Assuming it generates new text
2. Which of the following is the correct way to represent the start and end positions for answer span extraction in code?
easy
A. start_index and end_index as integers
B. start_word and end_word as strings
C. start_time and end_time as floats
D. start_char and end_char as booleans

Solution

  1. Step 1: Identify typical data types for positions

    Positions in text are usually represented by integer indices marking start and end locations.
  2. Step 2: Evaluate options

    Strings or booleans do not represent positions well; floats for time are unrelated to text spans.
  3. Final Answer:

    start_index and end_index as integers -> Option A
  4. Quick Check:

    Positions = integer indices [OK]
Hint: Positions in text are integer indices [OK]
Common Mistakes:
  • Using strings instead of integer indices
  • Confusing character positions with time values
  • Using booleans for position markers
3. Given the text: 'The cat sat on the mat.' and predicted start index = 1, end index = 4, what is the extracted answer span?
medium
A. 'cat sat on'
B. 'sat on the'
C. 'on the mat'
D. 'The cat sat'

Solution

  1. Step 1: Identify tokens and their indices

    Tokenizing the sentence: ['The'(0), 'cat'(1), 'sat'(2), 'on'(3), 'the'(4), 'mat.'(5)]. The indices given (1 to 4) refer to 0-based token positions.
  2. Step 2: Extract tokens from start to end index

    In standard extraction, take tokens[start:end] (end exclusive): tokens[1:4] = ['cat'(1), 'sat'(2), 'on'(3)] = 'cat sat on'.
  3. Final Answer:

    'cat sat on' -> Option A
  4. Quick Check:

    Extract tokens from start to end index = 'cat sat on' [OK]
Hint: Match indices to tokens carefully [OK]
Common Mistakes:
  • Confusing character indices with token indices
  • Off-by-one errors in slicing
  • Ignoring punctuation in tokens
4. You have a model that predicts start and end indices for answer spans but sometimes the end index is smaller than the start index. What is the best way to fix this bug?
medium
A. Ignore the prediction and return an empty answer
B. Always set end index to start index plus one
C. Swap the start and end indices if end < start
D. Use only the start index as the answer

Solution

  1. Step 1: Understand the problem with indices

    End index smaller than start index is invalid because answer spans must go forward in text.
  2. Step 2: Choose a fix that preserves valid spans

    Swapping start and end indices corrects the order and keeps the predicted span meaningful.
  3. Final Answer:

    Swap the start and end indices if end < start -> Option C
  4. Quick Check:

    Fix invalid spans by swapping indices [OK]
Hint: Swap indices if end < start to fix spans [OK]
Common Mistakes:
  • Ignoring invalid spans instead of fixing
  • Forcing fixed span length blindly
  • Using only one index loses answer context
5. In a question-answering system, the model outputs start logits and end logits for each token. How should you combine these to find the best answer span?
hard
A. Choose random start and end indices
B. Pick the token with the highest start logit only
C. Pick the token with the highest end logit only
D. Find the pair of start and end indices with the highest sum of start and end logits where start ≤ end

Solution

  1. Step 1: Understand logits for start and end tokens

    Start and end logits represent scores for each token being the start or end of the answer span.
  2. Step 2: Combine logits to find best span

    We look for the pair (start, end) with the highest combined score, ensuring start ≤ end to form a valid span.
  3. Final Answer:

    Find the pair of start and end indices with the highest sum of start and end logits where start ≤ end -> Option D
  4. Quick Check:

    Combine start and end logits to find best span [OK]
Hint: Sum start and end logits, ensure start ≤ end [OK]
Common Mistakes:
  • Ignoring end logits and using start only
  • Choosing invalid spans where end < start
  • Picking random indices without scores