Magicsheet logo

Detect Pattern of Length M Repeated K or More Times

Easy
100%
Updated 6/1/2025

Asked by 2 Companies

Detect Pattern of Length M Repeated K or More Times

What is this problem about?

In this Detect Pattern of Length M Repeated K or More Times coding problem, you are given an array of integers. You need to find if there is a sequence of length m that repeats at least k times consecutively. For example, if m=2 and k=3, you are looking for a pattern like [1, 2, 1, 2, 1, 2].

Why is this asked in interviews?

Companies like Hudson River Trading (HRT) ask this to test your ability to handle fixed-window comparisons and array indexing. It’s a test of observation. While it sounds like a complex string searching problem (like KMP), it can be solved with a simple linear scan, evaluating your ability to simplify algorithmic complexity.

Algorithmic pattern used

This problem uses an Enumeration pattern or Fixed Sliding Window. Instead of extracting and comparing subarrays, you can check if arr[i] == arr[i + m]. If this condition holds for mimes(k1)m imes (k - 1) consecutive indices, it means a pattern of length m has repeated k times.

Example explanation

Array: [1, 2, 1, 2, 1, 2, 3], m=2, k=3. We need 2imes(31)=42 imes (3 - 1) = 4 consecutive matches.

  1. Check arr[0] == arr[2]? Yes (1 == 1). Match count = 1.
  2. Check arr[1] == arr[3]? Yes (2 == 2). Match count = 2.
  3. Check arr[2] == arr[4]? Yes (1 == 1). Match count = 3.
  4. Check arr[3] == arr[5]? Yes (2 == 2). Match count = 4. Match count reached 4! Result: true.

Common mistakes candidates make

  • Nested Loops: Writing O(N3)O(N^3) code by checking all possible patterns and their repetitions.
  • Off-by-one: Not calculating the total required consecutive matches correctly (mimes(k1)m imes (k-1)).
  • Disjoint Patterns: Not realizing the repetitions must be consecutive.

Interview preparation tip

For "consecutive repetition" problems, look at the relationship between an element and its neighbor at a distance of m. If they match for a long enough stretch, the repetition is guaranteed.

Similar Questions