1
Why did RNNs struggle with long text?
Medium
In short: RNNs struggled with long text because they process tokens one by one and try to carry all previous information in a hidden state. As the sequence becomes longer, earlier information becomes weak, overwritten, or lost.

In an RNN, every word depends on the previous hidden state. This looks natural for sequences, but it creates a bottleneck: the model has to compress everything it has seen so far into one small state vector.

word 1 → hidden state → word 2 → hidden state → word 3 → ... → word 100

The problem becomes serious when the important word is far away from the current word. For example:

"The report that Amit prepared after multiple meetings with the AI team was excellent."

To understand was excellent, the model should connect it back to report. But after many intermediate words, the original signal may fade.

Main issues
  • Vanishing gradient: during training, early tokens receive very weak learning signals.
  • Sequential processing: tokens are processed one after another, so training is slow.
  • Fixed memory bottleneck: the hidden state has limited capacity to remember long context.
  • Long-range dependency issue: connecting distant words becomes difficult.
Interview line
RNNs were good for short sequences, but they struggled with long text because information had to flow step by step through hidden states, which caused memory loss, vanishing gradients, and slow sequential training.
2
What are LSTM and GRU, and why were they introduced?
Medium
In short: LSTM and GRU are improved versions of RNNs designed to remember long-term information better. They use gates to decide what to keep, what to forget, and what to pass forward.

Basic RNNs had difficulty remembering useful information over long sequences. LSTM and GRU were introduced to solve this by adding a controlled memory mechanism.

LSTM intuition

LSTM stands for Long Short-Term Memory. It has a memory cell and three main gates:

  • Forget gate: decides what old information to remove.
  • Input gate: decides what new information to store.
  • Output gate: decides what information to expose as output.
Old memory + New input ↓ Forget / Store / Output decisions ↓ Updated memory

Example: In a long paragraph, if the subject is report, the LSTM can keep that information in memory until it is needed later.

GRU intuition

GRU stands for Gated Recurrent Unit. It is a simpler version of LSTM with fewer gates:

  • Update gate: controls how much past information to keep.
  • Reset gate: controls how much past information to forget.

GRUs are usually faster and lighter than LSTMs, while still handling long dependencies better than vanilla RNNs.

Interview line
LSTM and GRU were introduced to overcome the vanishing gradient and memory limitation of basic RNNs. They use gating mechanisms to selectively remember or forget information across long sequences.
3
What problem did the encoder-decoder architecture solve?
Medium
In short: Encoder-decoder architecture solved sequence-to-sequence problems where the input and output can have different lengths, such as translation, summarization, and question answering.

Before encoder-decoder models, it was difficult to convert one sequence into another sequence when their lengths were different. For example, in translation, the English sentence and Hindi sentence may not have the same number of words or the same order.

Input: "How are you?" Output: "आप कैसे हैं?"

The encoder reads the input and converts it into a meaningful representation. The decoder then uses that representation to generate the output step by step.

Input sequence ↓ Encoder understands input ↓ Context representation ↓ Decoder generates output sequence
What it helped with
  • Machine translation
  • Text summarization
  • Question answering
  • Chatbot response generation
  • Speech-to-text and text-to-speech style tasks

However, early encoder-decoder models still had one major limitation: the whole input was compressed into one fixed-size vector. For long sentences, this caused information loss. Attention was introduced to solve that limitation.

Interview line
Encoder-decoder models made it possible to map variable-length input sequences to variable-length output sequences. The encoder understands the input, and the decoder generates the output.
4
What is attention, and why was it a breakthrough?
Medium
In short: Attention is a mechanism that lets a model focus on the most relevant input tokens while generating or understanding each token. It was a breakthrough because the model no longer had to depend only on one compressed vector for the entire input.

In early encoder-decoder models, the encoder compressed the full input sentence into a single context vector. This worked for short sentences, but for long sentences, important information could be lost.

Old approach: Full input sentence → one compressed vector → output

Attention changed this. Instead of using only one fixed representation, the decoder can look back at all input tokens and assign importance scores to them.

New approach: For each output word → focus on relevant input words

Example: while translating Amit is preparing for an AI interview, when generating the word related to Amit, the model focuses on Amit. When generating the word related to preparing, it focuses on preparing.

Why it was a breakthrough
  • It reduced information loss in long sequences.
  • It improved translation and summarization quality.
  • It helped the model capture long-range dependencies.
  • It made model behavior more interpretable through attention weights.
  • It later became the foundation of Transformers.
Interview line
Attention was a breakthrough because it allowed the model to dynamically focus on relevant tokens instead of relying on a single compressed representation of the whole input.
5
What is self-attention?
Medium
In short: Self-attention lets every token in a sequence compare itself with every other token in the same sequence to build context-aware meaning.

The word bank can mean a financial bank or a river bank. Its meaning depends on surrounding words. Self-attention helps the model understand this by allowing each token to look at other tokens.

Sentence 1: "I deposited money in the bank." bank attends to → deposited, money Sentence 2: "I sat near the bank of the river." bank attends to → river, near

So the same word can get a different representation depending on context.

How it works intuitively

For each token, the model asks:

  • Which other tokens are important for understanding me?
  • How strongly should I connect to them?
  • How should my meaning change based on them?

Another example:

"The animal did not cross the road because it was tired." The token "it" attends more to "animal" than "road".

This helps with pronoun resolution, word sense disambiguation, grammar relationships, and long-distance dependencies.

Interview line
Self-attention creates context-aware token representations by letting each token attend to all other tokens in the same sequence and update its meaning based on relevance.
6
What is multi-head attention?
Hard
In short: Multi-head attention means running multiple self-attention mechanisms in parallel, where each head learns a different type of relationship between tokens.

A single attention head may focus on one kind of relationship. But language has many relationships at the same time: grammar, meaning, entity reference, position, topic, and long-range dependency.

Multi-head attention solves this by using several attention heads together.

Input tokens ↓ Head 1: grammar relation Head 2: entity relation Head 3: long-range dependency Head 4: semantic meaning ↓ Concatenate outputs ↓ Final representation
Example
"The animal did not cross the road because it was tired."
  • One head may connect it to animal.
  • One head may understand the cause-effect relation with because.
  • One head may focus on grammatical structure.
  • One head may focus on the overall sentence meaning.

The outputs of all heads are combined, giving the model a richer understanding than a single attention head.

Interview line
Multi-head attention allows the model to look at the same sequence from multiple perspectives at once. Different heads can learn different relationships, making the final token representation richer and more expressive.
7
Why did transformers replace RNNs for many NLP tasks?
Hard
In short: Transformers replaced RNNs because they handle long-range dependencies better, process tokens in parallel, scale efficiently, and use self-attention to capture context more effectively.

RNNs process text sequentially. This means token 10 cannot be processed before token 9. That makes training slow and difficult to parallelize.

RNN: token 1 → token 2 → token 3 → token 4 Transformer: token 1, token 2, token 3, token 4 processed in parallel

Transformers use self-attention, so every token can directly connect with every other token. This makes it easier to learn long-distance relationships.

Why Transformers became better
  • Parallel processing: much faster training on GPUs/TPUs.
  • Better long-context handling: tokens can directly attend to distant tokens.
  • Scalability: performance improves strongly with more data and parameters.
  • Transfer learning: pretrained Transformer models can be fine-tuned for many tasks.
  • Flexible architecture: encoder-only, decoder-only, and encoder-decoder variants.

This is why models like BERT, GPT, T5, and LLaMA are Transformer-based rather than RNN-based.

Interview line
Transformers replaced RNNs because they removed the sequential bottleneck, improved long-range dependency learning, trained faster through parallelism, and scaled much better with large data and compute.
8
What is positional encoding and why is it needed?
Hard
In short: Positional encoding adds word-order information to token embeddings because self-attention alone does not naturally understand the order of tokens.

Transformers process all tokens in parallel. This is good for speed, but it creates one issue: the model does not automatically know which word came first, second, or third.

"Dog bites man" "Man bites dog"

Both sentences have the same words, but the meaning is completely different because the order is different. Positional encoding helps the model understand this order.

How it is added

Each token gets two kinds of information:

Final input representation = Token embedding + Positional encoding

Token embedding tells the model what the word means. Positional encoding tells where the word appears in the sequence.

Types
  • Sinusoidal positional encoding: fixed mathematical position signals used in the original Transformer.
  • Learned positional embeddings: position vectors learned during training.
  • Relative positional encoding: focuses on distance between tokens rather than absolute position.
  • RoPE: commonly used in modern LLMs to encode relative position using rotation.
Interview line
Positional encoding is needed because Transformers process tokens in parallel and self-attention is order-agnostic by default. It injects sequence order so the model can understand grammar and meaning correctly.
9
What is the difference between encoder-only, decoder-only, and encoder-decoder transformers?
Hard
In short: Encoder-only models are best for understanding tasks, decoder-only models are best for generation tasks, and encoder-decoder models are best for sequence-to-sequence tasks.
1. Encoder-only Transformers

Encoder-only models read the full input at once using bidirectional attention. This means every token can look at tokens on both left and right sides.

Example model: BERT Best for: classification, embeddings, NER, sentiment analysis

They are strong when the task is to understand text rather than generate long text.

2. Decoder-only Transformers

Decoder-only models generate text autoregressively, one token at a time. They use masked self-attention, so the model cannot look at future tokens while predicting the next token.

Example models: GPT, LLaMA, Mistral Best for: chat, completion, coding, reasoning, content generation

This is the main architecture behind modern LLMs.

3. Encoder-decoder Transformers

These models have both an encoder and a decoder. The encoder understands the input, and the decoder generates the output.

Example models: T5, BART Best for: translation, summarization, question answering, text-to-text tasks
Simple comparison
Encoder-only → understand text Decoder-only → generate text Encoder-decoder → transform one sequence into another
Interview line
BERT-style encoder models are used for understanding, GPT-style decoder models are used for generation, and T5/BART-style encoder-decoder models are used when input text needs to be transformed into output text.
10
How did transformers lead to modern LLMs?
Hard
In short: Transformers led to modern LLMs because their self-attention architecture scales very well with large data, large models, and parallel training. This made it possible to train models like GPT, BERT, T5, LLaMA, and other large language models.

The Transformer architecture solved many limitations of RNNs. It allowed models to process tokens in parallel, capture long-range dependencies using attention, and train efficiently on massive datasets.

RNN era ↓ LSTM / GRU ↓ Encoder-decoder ↓ Attention ↓ Transformer ↓ Pretrained language models ↓ Modern LLMs
Key reasons Transformers enabled LLMs
  • Parallel training: made large-scale training practical.
  • Self-attention: improved context understanding.
  • Scalability: larger models and more data improved performance significantly.
  • Pretraining: models could learn general language patterns from huge text corpora.
  • Fine-tuning and instruction tuning: made models useful for specific tasks and conversations.
  • Decoder-only architecture: enabled next-token prediction at massive scale.

Modern LLMs are mostly Transformer-based. They are trained to predict the next token, then improved using instruction tuning, supervised fine-tuning, reinforcement learning from human feedback, preference optimization, and tool/RAG integrations.

Interview line
Transformers became the foundation of modern LLMs because they combined self-attention, parallelism, and scalability. Once trained on massive text data with next-token prediction, they became powerful general-purpose language models.