πŸͺ

Essential cookies keep authentication working. With your permission, we also use analytics to improve the product. Privacy Policy

DDataEngPrep.tech
Home
PricingBlog
Sign InStart Free→
Home/Questions/Python/Coding/Write a function to detect anomalies in streaming data using a sliding window.

Write a function to detect anomalies in streaming data using a sliding window.

Python/Codinghard0.4 min read

Reviewed by Aditya Kumar Β· Last reviewed 2026-03-24

**Approach:** Maintain window (deque). Compute rolling mean, std. Flag if value beyond k*std (e.g., 3-sigma). For streaming: update incrementally (Welford's algo for running mean/var). **Robustness:** Mean/std sensitive to outliers. Use median + MAD for robust. Or EWMA for...

πŸ€– Analyze Your Answer
Frequency
Low
Asked at 1 company
Category
179
questions in Python/Coding
Difficulty Split
127E|24M|28H
in this category
Total Bank
1,863
across 7 categories
Part of the SQL Window Functions Interview Preparation Hub
Explore SQL Window Functions Hub→
Reported in Interview Loops AtClick to view company-specific questions
🏒 Disney+ Hotstar Interview Hubβ†’
Key Concepts Tested
window
Expert Answer
73 words

Approach: Maintain window (deque). Compute rolling mean, std. Flag if value beyond k*std (e.g., 3-sigma). For streaming: update incrementally (Welford's algo for running mean/var).

Robustness: Mean/std sensitive to outliers. Use median + MAD for robust. Or EWMA for trend. Alert on anomaly rate, not single spike.

def detect_anomalies(values, window=10, k=3):
for i in range(window, len(values)):
w = values[i-window:i]
if np.std(w) > 0 and abs(values[i] - np.mean(w)) > k * np.std(w):
yield i, values[i]

⚑
Pro Tip

Red Flag: 3-sigma on heavy-tailed data. Pro-Move: 'We use MAD + median; alert when anomaly rate > 5% over 1 min.'

Related Python/Coding Questions

easyWhat are traits in Scala, and how are they different from classes?FreemediumWrite a Python function to check if a string is a palindrome.FreeeasyWhat is the difference between a list and a tuple in Python?FreeeasyExplain the difference between shallow copy and deep copy in Python.FreeeasyWrite a Python function to find the first non-repeating character in a string.Free

According to DataEngPrep.tech, this is one of the most frequently asked Python/Coding interview questions, reported at 1 company. DataEngPrep.tech maintains an editor-reviewed database of 1,863 data engineering interview questions across 7 categories.

← Back to all questionsMore Python/Coding questions β†’
Topic Hubs
All Topic Hubs (12 Competencies) β†’All 1,863+ QuestionsSQL Interview QuestionsApache Spark QuestionsPySpark QuestionsPython for Data EngineersSystem Design InterviewData Modeling & WarehousingStreaming (Kafka/Flink)SQL Window FunctionsETL Pipeline Design
Company Hubs
All Company Hubs (100+ Companies) β†’AmazonGoogleMetaDatabricksSnowflakeNetflixUberStripeMicrosoftAppleGoldman SachsAWS Engineering
Academies & Guides
All Guides & ArticlesData Engineering AcademyForward Deployed AI AcademyClient Simulation StudioApache Airflow QuestionsAzure Data EngineerBehavioral & STAR MethodCloud & Distributed Tools
AI Tools & Products
Answer Analyzer (Free)AI Mock InterviewsSQL PlaygroundATS Resume AnalyzerOffline PDF Answer VaultsMembership & Pricing
Company
About & Editorial PolicyContact UsAI DisclosureDisclaimerTerms of ServicePrivacy Policy
Β© 2026 DataEngPrep.tech. All rights reserved.
AboutBlogContactDisclaimer