Skip to content
Vibhrav Jha.Let’s talk
All projectsDistributed Systems

Spark Analytics

From programming-problem records to reusable analysis

My contributionSpark analysis, query preparation and difficulty-model experimentation.

FieldDistributed Systems / Data Engineering / ML & AI

View repository

Academic project

Architecture diagram of Spark processing, SQL and analysis

The work
behind the interface.

Turned 8,573 programming problems into a queryable Spark dataset, with cached analysis, difficulty modeling and schema-aware model-assisted SQL.

  • Python
  • Spark
  • Hive
  • HDFS
  • MLlib
  • Gemini

The result

A shared dataset explored through multiple Spark APIs, an MLlib regressor and a schema-aware SQL interface.

The problem

Analyzed 8,573 CodeContests problems through Spark RDDs, DataFrames and SQL, with a difficulty model and model-assisted querying.

A decision worth examining

Use compatible bucketing and caching for repeated analysis, keeping query layout part of the performance reasoning.

Read the README.

View original README
READMEArchitecture, setup and usage

Competitive Programming Analytics with Apache Spark

A distributed data processing and machine learning system built on Apache Spark for analyzing competitive programming problems and solutions. Features natural language query interface powered by LLMs, performance optimization through bucketing and caching, and predictive analytics using decision tree models.

Overview

This system processes the DeepMind CodeContests dataset containing thousands of competitive programming problems from platforms like Codeforces, along with their solutions in multiple languages. It demonstrates advanced Spark capabilities including distributed SQL, ML pipelines, and query optimization.

Architecture

The system consists of six containerized services:

  • Jupyter Notebook: Interactive development environment
  • Spark Master (Boss): Cluster coordination and job scheduling
  • Spark Workers (2 replicas): Distributed computation nodes
  • HDFS NameNode: Metadata management
  • HDFS DataNodes (2 replicas): Distributed storage

Key Features

📊 Multi-Model Data Processing

  • RDD API: Low-level distributed data operations
  • DataFrame API: High-level structured data processing
  • Spark SQL: Declarative querying with Hive integration
  • Seamless translation between all three APIs

🚀 Performance Optimization

  • Bucketing: Pre-partitioned data by language (4 buckets)
  • Caching: In-memory storage for iterative queries
  • Query Optimization: Eliminates shuffle operations through smart bucketing
  • 80%+ faster query execution on cached data

🤖 Natural Language Query Interface

  • LLM-powered SQL generation from English questions
  • Programmatic Gemini API integration
  • Automatic schema discovery and prompt engineering
  • Zero-shot query translation with 100% accuracy

🎯 Machine Learning Pipeline

  • Decision tree regression for difficulty rating prediction
  • Feature engineering with VectorAssembler
  • Hyperparameter tuning and overfitting analysis
  • Model evaluation with R² scoring

Technical Stack

  • Compute Engine: Apache Spark 3.x (PySpark)
  • Data Storage: Apache Hadoop HDFS
  • Data Warehouse: Apache Hive
  • ML Framework: Spark MLlib
  • Data Formats: JSON Lines, CSV, Parquet
  • LLM Integration: Google Gemini API (gemini-2.5-flash)
  • Development: Jupyter Lab
  • Containerization: Docker, Docker Compose

Dataset

DeepMind CodeContests dataset featuring:

  • 8,573 problems from competitive programming platforms
  • Solutions in 13+ programming languages
  • Metadata: Difficulty ratings, time/memory limits, test cases
  • Sources: Codeforces, CodeChef, and other platforms

Analysis Capabilities

1. Multi-API Querying

Demonstrates proficiency across all three Spark programming models:

# RDD API
count_rdd = problems_df.rdd \
    .filter(lambda row: row.cf_rating >= 1600 and 
            row.has_private_tests and "_A." in row.name) \
    .count()

# DataFrame API
count_df = problems_df \
    .filter((col("cf_rating") >= 1600) & 
            col("has_private_tests") & 
            col("name").contains("_A.")) \
    .count()

# Spark SQL
count_sql = spark.sql("""
    SELECT COUNT(*) FROM problems 
    WHERE cf_rating >= 1600 
    AND has_private_tests = true 
    AND name LIKE '%_A.%'
""").collect()[0][0]

2. Complex Join Analysis

Cross-references solutions with problem metadata and source platforms:

SELECT COUNT(*) 
FROM solutions s
JOIN problems p ON s.problem_id = p.problem_id
JOIN sources src ON p.source = src.id
WHERE s.language = 'PYTHON3' 
  AND s.is_correct = true
  AND src.name = 'CODEFORCES'

3. Difficulty Categorization

Classifies problems using CASE expressions:

SELECT 
  CASE 
    WHEN difficulty <= 5 THEN 'Easy'
    WHEN difficulty <= 10 THEN 'Medium'
    ELSE 'Hard'
  END as category,
  COUNT(*) as count
FROM problems
GROUP BY category

Results: 409 Easy, 5,768 Medium, 2,396 Hard problems

4. Natural Language Queries

LLM-powered query interface enabling non-technical users:

def human_query(english_question):
    # Construct prompt with schema and question
    prompt = f"""
    Given these tables: {schema_info}
    Convert to SQL: {english_question}
    Return only the query, no formatting.
    """
    
    # Generate SQL via Gemini API
    response = client.models.generate_content(
        model='gemini-2.5-flash',
        contents=prompt,
        config={'temperature': 0}
    )
    
    # Execute and return result
    result = spark.sql(response.text).collect()[0][0]
    return int(result)

# Usage
java_count = human_query("How many JAVA solutions are there?")
max_memory = human_query("What is the maximum memory limit in bytes?")

Performance Analysis

Query Plan Optimization

Without Bucketing: Requires expensive shuffle/exchange operations

Exchange hashpartitioning(language)
  HashAggregate(keys=[language], functions=[partial_count(1)])

With Bucketing: No shuffle needed - data pre-partitioned

HashAggregate(keys=[language], functions=[count(1)])
  HashAggregate(keys=[language], functions=[partial_count(1)])

Caching Performance

Benchmark results for average calculations on filtered dataset:

RunCaching StatusTime (seconds)
1stNot cached0.909
2ndCached1.413
3rdCached (warm)0.196

Result: 78% performance improvement with warm cache

Machine Learning Pipeline

Problem Difficulty Prediction

Trained decision tree regressor to estimate Codeforces ratings for problems with missing difficulty scores.

Features: difficulty, time_limit, memory_limit_bytes
Target: cf_rating

Data Split:

  • Training: Even-numbered problem IDs with known ratings
  • Testing: Odd-numbered problem IDs with known ratings
  • Prediction: Problems with missing ratings

Results:

  • Average training rating: ~1,450
  • Average test rating: ~1,455
  • Average predicted rating: ~1,380

Insight: Problems with missing ratings appear slightly less challenging than rated problems.

Overfitting Analysis

![Overfitting Analysis]

Trained models with varying tree depths (1-30) to analyze bias-variance tradeoff:

depths = [1, 2, 3, 5, 7, 10, 15, 20, 25, 30]
results = []

for depth in depths:
    pipeline = Pipeline(stages=[
        VectorAssembler(inputCols=features, outputCol="features"),
        DecisionTreeRegressor(maxDepth=depth, labelCol="cf_rating")
    ])
    
    model = pipeline.fit(train_df)
    train_r2 = evaluator.evaluate(model.transform(train_df))
    test_r2 = evaluator.evaluate(model.transform(test_df))
    
    results.append({'depth': depth, 'train': train_r2, 'test': test_r2})

Key Findings:

  • Optimal depth ~7: Best test performance (R² ≈ 0.45)
  • Depth >10: Overfitting evident (train R² increases, test R² plateaus/decreases)
  • Depth <5: Underfitting (both train and test R² suboptimal)

Data Pipeline

  1. Extraction: Download DeepMind CodeContests dataset
  2. Transformation: Split into problems and solutions JSONL files
  3. Loading: Upload to HDFS with controlled replication
  4. Schema Definition: Create Hive tables and views
  5. Optimization: Apply bucketing strategies
  6. Analysis: Execute distributed queries and ML pipelines

Deployment

Build Images

docker build -f p5-base.Dockerfile -t spark-analytics-base .
docker build -f notebook.Dockerfile -t spark-analytics-nb .
docker build -f namenode.Dockerfile -t spark-analytics-nn .
docker build -f datanode.Dockerfile -t spark-analytics-dn .
docker build -f boss.Dockerfile -t spark-analytics-boss .
docker build -f worker.Dockerfile -t spark-analytics-worker .

Start Cluster

export PROJECT=spark-analytics
export GEMINI_API_KEY='your-api-key'
docker compose up -d

Access Jupyter Lab

http://localhost:5000/lab

Implementation Highlights

Hive Integration

spark = SparkSession.builder \
    .appName("competitive-programming-analytics") \
    .master("spark://boss:7077") \
    .config("spark.sql.warehouse.dir", "hdfs://nn:9000/user/hive/warehouse") \
    .enableHiveSupport() \
    .getOrCreate()

Bucketing Strategy

solutions_df.write \
    .mode("overwrite") \
    .bucketBy(4, "language") \
    .saveAsTable("solutions")

LLM Prompt Engineering

  • Dynamic schema introspection
  • Deterministic SQL generation (temperature=0)
  • Robust output parsing
  • Error handling and validation

ML Pipeline Design

  • Feature vector assembly
  • Train/test split strategy
  • Hyperparameter grid search
  • Performance visualization

Skills Demonstrated

  • Distributed Computing: Spark cluster management, job optimization
  • Data Engineering: ETL pipelines, Hive integration, bucketing strategies
  • Query Optimization: Caching, partitioning, shuffle elimination
  • Machine Learning: Regression, feature engineering, overfitting analysis
  • LLM Integration: API orchestration, prompt engineering, SQL generation
  • Performance Engineering: Benchmarking, profiling, optimization
  • Data Visualization: matplotlib, result interpretation
  • Container Orchestration: Multi-service Docker deployment

Technologies

  • Apache Spark (RDD, DataFrame, SQL APIs)
  • Apache Hive (data warehousing)
  • Apache Hadoop HDFS (distributed storage)
  • Spark MLlib (decision trees, pipelines)
  • Google Gemini API (LLM integration)
  • PySpark, pandas, matplotlib
  • Docker, Docker Compose

A comprehensive demonstration of modern big data processing, machine learning, and AI-powered analytics.

Multi-Source ELT Pipeline

  1. Aranyaka wildlife-image application interface

    Fullstack

    Aranyaka

  2. DermaNet hackathon prototype showing classification output and a generated explanation

    ML & AI

    DermaNet

  3. AI-generated concept illustration of source documents connected to retrieved evidence and an answer

    ML & AI

    Private-Data RAG

  4. Before It Breaks dashboard separating real weather from synthetic equipment telemetry and estimated costs

    ML & AI

    Before It Breaks

  5. Architecture diagram of simulated stock records flowing through Kafka to HDFS

    Distributed Systems

    Stock Streaming Engine

  6. Vyaya dashboard with clearly labeled synthetic demonstration data

    Fullstack

    Vyaya

  7. Study assistant interface with retrieved source context

    ML & AI

    Fine-Tuning vs. RAG

  8. Architecture diagram of NOAA data, a Cassandra cluster and a query service

    Distributed Systems

    Distributed Weather Data System

  9. Distributed Property Lookup Service project image

    Distributed Systems

    Distributed Property Lookup Service

  10. Architecture diagram connecting MySQL, a gRPC service and HDFS storage

    Distributed Systems

    HDFS & SQL Pipeline

  11. Architecture diagram of Spark processing, SQL and analysis

    Distributed SystemsViewing

    Spark Analytics

  12. Multi-Source ELT Pipeline project image

    Data Engineering

    Multi-Source ELT Pipeline

  13. Wisconsin schools in their geographic context — explanatory architecture diagram

    Data Engineering

    GCP Geospatial Pipeline

  14. Chinook Music Store SQL Analytics project image

    Data Engineering

    Chinook Music Store SQL Analytics

  15. Yelp Geospatial Analytics project image

    Data Engineering

    Yelp Geospatial Analytics

  16. E-Commerce Semantic Search Engine project image

    Data Engineering

    E-Commerce Semantic Search Engine

  17. Retail Sales Forecasting project image

    ML & AI

    Retail Sales Forecasting

  18. Making tool dependencies inspectable — explanatory architecture diagram

    ML & AI

    AI Agent Tool Dependency Graph

  19. From public records to operational questions — explanatory architecture diagram

    Data Engineering

    Palantir Foundry and AIP Build Challenge

  20. StudyFlow study-session dashboard

    Fullstack

    StudyFlow

  21. Habit Command Center planning dashboard

    Fullstack

    Habit Command Center

  22. Interfaces across web and mobile — explanatory architecture diagram

    Fullstack

    React Web & Mobile

  23. Startup Scout project image

    Fullstack

    Startup Scout

  24. Inventory-Based Recipe Planner project image

    Fullstack

    Inventory-Based Recipe Planner

  25. Concurrent Word Count and Performance Benchmarking project image

    Data Engineering

    Concurrent Word Count

  26. Data structures matched to their assumptions — explanatory architecture diagram

    Computing Foundations

    Algorithm Design

  27. Campus Navigator project image

    Fullstack

    Campus Navigator

  28. i-Songly: Song Management Engine project image

    Fullstack

    i-Songly

  29. An experiment that can resume — explanatory architecture diagram

    ML & AI

    CIFAR-100 with LeNet

  30. Fashion-MNIST Neural Network Classifier project image

    ML & AI

    Fashion-MNIST Classifier

  31. PCA Eigenfaces and Image Reconstruction project image

    ML & AI

    PCA Eigenfaces

  32. Two routes to the same regression objective — explanatory architecture diagram

    ML & AI

    Lake Mendota Ice Regression

  33. Bayesian Language Detection project image

    ML & AI

    Bayesian Language Detection

  34. Explanatory diagram of the video-fingerprint benchmark: 25 sources, 75 transformed variants and 1,875 comparisons.

    ML & AI

    Video-Fingerprint Benchmark

All projects