Skip to content

Interview Questions — Sentiment Analysis on Tweets

These eight questions cover the full project: data decisions, feature engineering, model selection, evaluation, and real-world deployment considerations. Work through each before looking at the answer.


Q1. What is TF-IDF and why does it work better than raw word counts for sentiment classification?

Show answer

TF-IDF stands for Term Frequency — Inverse Document Frequency. It is a numerical weighting scheme for text features.

  • TF (Term Frequency): how often a word appears in a specific document (tweet), normalised by the total number of words in that document.
  • IDF (Inverse Document Frequency): log(total documents / number of documents containing the word). Words that appear in many documents receive a lower IDF weight.
  • TF-IDF score = TF × IDF.

Why it outperforms raw counts:

Raw word counts give high weights to frequent words like "the," "from," "my" — words that appear across all sentiment classes and carry no sentiment signal. A model trained on raw counts wastes most of its capacity on these uninformative features.

TF-IDF automatically downweights common words (high document frequency → low IDF → low TF-IDF) and upweights rare, discriminative words. "Furious" appears in negative tweets but not elsewhere — high TF-IDF weight in negative tweets. "From" appears everywhere — near-zero TF-IDF weight everywhere.

The practical effect: TF-IDF features push the model to focus on the words that actually carry sentiment information, which is exactly what you want.


Q2. Why did you include bigrams in your TF-IDF features, and what do they add over unigrams alone?

Show answer

Bigrams (consecutive word pairs) capture phrase-level patterns that single words miss.

Consider the word "broke." Alone, it could mean anything. As part of "broke after" it strongly signals product failure. Similarly, "five stars" (positive signal), "still waiting" (negative signal), and "every expectation" (as in "exceeded every expectation" — positive) are more informative as bigrams than as individual words.

In short text like tweets, the vocabulary per document is small, so individual words are ambiguous. Bigrams add context without requiring a full sequence model.

The tradeoff: - Bigrams multiply the feature space. With 5000 max features and ngram_range=(1,2), the vectoriser selects the 5000 most informative features from all unigrams and bigrams combined. - Bigrams are sparse — each bigram appears far less often than its constituent words, so min_df=2 (ignore features appearing in fewer than 2 documents) becomes important to avoid noise.

In practice, adding bigrams to tweet sentiment classification consistently improves macro F1 by 2–5 points over unigrams alone, because the most discriminative sentiment signals are often phrases.


Q3. Why is Multinomial Naive Bayes a strong baseline for text classification despite its "naive" independence assumption?

Show answer

Naive Bayes assumes that all features (words) are conditionally independent given the class. In reality, words are highly correlated — "absolutely" almost always co-occurs with "amazing" in positive tweets. So the independence assumption is demonstrably wrong.

Despite this, Naive Bayes works well on text for two reasons:

1. The independence assumption rarely changes the predicted class, even when it gets probabilities wrong. For classification, you only need the model to rank the correct class highest. Even biased probability estimates often produce the right argmax — especially when the true signal words are strong and frequent.

2. The independence assumption is partially self-correcting with TF-IDF. TF-IDF already normalises term frequencies and downweights common co-occurring words. The features passed to Naive Bayes are already somewhat decorrelated in their discriminative power.

The practical reasons to start with Naive Bayes: - Trains in milliseconds even on millions of documents. - Has one hyperparameter (alpha for smoothing) — very easy to tune. - Provides probability estimates for all classes, not just the argmax. - Frequently scores within 5–10% of Logistic Regression, making it a reliable ceiling-check for whether the task is learnable at all.

If Naive Bayes fails, the problem is likely the features or the data, not the model choice.


Q4. Your model struggles with neutral tweets — why, and how is this reflected in the evaluation metrics?

Show answer

Neutral tweets have the lowest F1 score (0.84 vs 0.97 for negative and 0.96 for positive) for a structural reason: they lack discriminative vocabulary.

Why neutral is hard:

A TF-IDF model learns weights for individual words and phrases. Positive tweets contain words like "amazing," "outstanding," "five stars." Negative tweets contain "broke," "refund," "furious." These words are almost exclusive to one class — TF-IDF assigns them high weights.

Neutral tweets contain words like "ordered," "arrived," "available," "comparing." These words also appear in the context of positive and negative tweets ("I ordered a product that broke," "I arrived to find my delivery"). They are not class-exclusive, so TF-IDF assigns them low weights.

When the model scores a neutral tweet, it finds low-weight features throughout and defaults to whichever class the surrounding vocabulary most resembles. "Returned" pulls toward negative. Product and brand names pull toward positive (because new purchases dominate positive training examples).

In the confusion matrix: Neutral misclassifications split approximately evenly between negative (7.5%) and positive (7.5%) — confirming there is no systematic directional bias. The model is not confused about the direction of neutral; it is confused about the absence of direction.

What helps: - Adding negation-specific features ("just ordered," "just arrived" — neutral action phrases) - BERT, which reads the full sentence and understands that "I ordered a product" is a factual statement, not an expression of sentiment - More training data for neutral specifically (currently 200 samples vs 400 for each other class)


Q5. How would you handle sarcasm in a sentiment analysis system?

Show answer

Sarcasm is the hardest problem in sentiment analysis because the surface-level vocabulary is misleading. "Oh great, another broken product" uses a positive word ("great") to express a negative sentiment.

TF-IDF cannot handle this — it treats "great" as a positive signal regardless of context.

Approaches in order of increasing complexity:

1. Rule-based sarcasm markers (cheap, limited coverage) Patterns like "Oh great," "just brilliant," "as always," combined with negative context words trigger a sarcasm flag. Works only for known patterns.

2. Contextual word embeddings (significant improvement) Models like BERT read the full sentence bidirectionally. "Oh great, another broken product" — BERT sees "another broken" as context for "great" and learns that this pattern is sarcastic from pre-training on diverse internet text. Fine-tuned BERT models score substantially higher on sarcasm-aware benchmarks like the irony task in tweet_eval.

3. Dedicated sarcasm detection as a pre-filter Train a binary sarcasm classifier first. For tweets flagged as sarcastic, invert the predicted sentiment (positive → negative). This is not robust but adds a practical layer.

4. Ensembles and multi-task learning Train a model jointly on sentiment and irony detection. Shared representations help the sentiment task implicitly.

In production, the honest answer is that sarcasm detection remains an unsolved problem. Any deployed sentiment system should include human review for cases with low confidence scores, and should report sarcasm limitations to stakeholders explicitly.


Q6. The dataset is slightly imbalanced (400 positive, 400 negative, 200 neutral). How did you address this, and what else could you do?

Show answer

What was done:

  • Stratified train-test split (stratify=y): preserves the 40/40/20 class ratio in both training and test sets. Without this, random chance could produce a test set with a different distribution, making metrics unreliable.
  • Macro F1 as the primary metric: reports performance equally across all three classes regardless of frequency. Accuracy and weighted F1 would both inflate results by giving more weight to the majority classes.

What else could be done:

Oversampling the minority class (SMOTE): For tabular data, SMOTE generates synthetic minority samples. For text data, this is harder — you cannot interpolate between TF-IDF vectors and produce valid text. Alternatives include back-translation (translate neutral tweets to another language and back) or paraphrasing with an LLM.

class_weight='balanced' in the classifier: LogisticRegression and LinearSVC both accept class_weight='balanced', which weights each sample inversely proportional to its class frequency. This penalises errors on the neutral class more heavily during training.

LogisticRegression(class_weight="balanced", max_iter=1000, multi_class="multinomial")

Threshold tuning: If you have probability estimates (predict_proba()), lower the decision threshold for the neutral class so more tweets get classified as neutral. This trades precision for recall on the minority class.

Collect more neutral data: The most direct fix for class imbalance is more labelled data in the minority class. In a real project, this means targeted collection of neutral-labelled tweets.


Q7. What would change if you needed to classify tweets in real-time as they stream in?

Show answer

The modeling approach stays the same. The deployment infrastructure changes significantly.

What works the same: - The trained sklearn Pipeline (clean → TF-IDF → classify) can classify a single tweet in under 1 millisecond. Throughput is not the constraint. - pipeline.predict(pd.Series([new_tweet])) works on a single sample.

What changes:

1. Serving the model The pipeline must be serialised (via joblib.dump) and loaded into a serving layer — a REST API (FastAPI, Flask), a serverless function, or a streaming processor like Kafka Streams or Apache Flink.

2. Input validation Streaming data is messy. You need schema validation (is this actually text?), language detection (is it English?), and deduplication (the same viral tweet arrives many times).

3. Concept drift monitoring Slang evolves. New product names appear. Brand names change. The vocabulary in the TF-IDF vocabulary that was fitted at training time becomes stale. You need to monitor prediction confidence distributions over time and retrain periodically.

4. Latency vs throughput tradeoff For high-volume tweet streams, batch micro-batching (classify tweets in groups of 100) is more efficient than classifying one at a time, even if latency increases slightly.

5. Model versioning When you retrain with new data, you need a way to A/B test the new model against the old one in production without taking the system offline.

The tweet classification model itself is simple and fast. The challenge at real-time scale is everything around the model: ingestion, serving, monitoring, and drift detection.


Q8. How would BERT improve over TF-IDF + Logistic Regression, and what is the tradeoff?

Show answer

Where BERT improves:

1. Context understanding BERT reads every word in the context of every other word in the sentence. "Not great" is understood as negative even though "great" is positive. TF-IDF treats them as two independent features — "not" gets low weight, "great" gets high positive weight.

2. Sarcasm and negation BERT learns from pre-training on diverse internet text that includes sarcasm. It is not immune to sarcasm, but it handles it substantially better than bag-of-words approaches.

3. Subword tokenisation BERT's tokeniser handles out-of-vocabulary words by splitting them into subwords ("unhappy" → "un" + "##happy"). TF-IDF silently drops OOV tokens — if "unfollowed" is not in the training vocabulary, it contributes nothing. BERT retains the meaning from the subword pieces.

4. Transfer learning from 100s of millions of documents BERT is pre-trained on BooksCorpus and Wikipedia. It arrives to your task already knowing that "furious" is an emotion, that "refund" often appears in complaint contexts, and that "blew me away" is positive. Fine-tuning adapts this knowledge to your specific labels with relatively little task-specific data.

On the tweet_eval sentiment benchmark, fine-tuned BERT-based models score 5–15 macro F1 points higher than TF-IDF + Logistic Regression on the same data.

The tradeoffs:

Dimension TF-IDF + LR BERT fine-tuned
Training time Seconds 30–60 minutes on GPU
Inference speed ~30,000 tweets/second (CPU) ~200–500 tweets/second (GPU)
Hardware requirement Any laptop CPU GPU strongly recommended
Interpretability Coefficient inspection Attention weights (partial)
Accuracy on clean data 94% ~97–98%
Accuracy on real tweets Drops significantly Drops less
Library complexity scikit-learn only transformers, torch/tensorflow

When to choose each: - TF-IDF + LR: daily batch processing, resource-constrained environments, learning the NLP workflow, prototyping - BERT fine-tuning: production systems on real social media data, when sarcasm or negation matters, when you have GPU infrastructure


← Model Building | Back to Project Overview →