1D NumPy Strides

Data Wrangling

Difficulty: 3 | Problem written by Mr. Umair

Educational Resource: https://cs231n.github.io/python-numpy-tutorial/


Given a 1D Numpy array, your function will return a 2D matrix using strides given the length of the strides and a window length. For example, if we have a window length of 5 and stride length of 3, than we take the first 5 elements of the array, then we skip 3 elements (starting from the beginning) and then take the next 5 elements again.


Your function will return a 2D list after computation of strides on the given input values.

Sample Input:
<class 'numpy.ndarray'>
arr: [ 1 2 3 4 5 6 7 8 9 10]
<class 'int'>
lengthOfStride: 2
<class 'int'>
windowLength: 4

Expected Output:
<class 'list'>
[[1, 2, 3, 4], [3, 4, 5, 6], [5, 6, 7, 8], [7, 8, 9, 10]]

This is a premium problem, to view more details of this problem please sign up for MLPro Premium. MLPro premium offers access to actual machine learning and data science interview questions and coding challenges commonly asked at tech companies all over the world

MLPro Premium also allows you to access all our high quality MCQs which are not available on the free tier.

Not able to solve a problem? MLPro premium brings you access to solutions for all problems available on MLPro

Get access to Premium only exclusive educational content available to only Premium users.

Have an issue, the MLPro support team is available 24X7 to Premium users.

This is a premium feature.
To access this and other such features, click on upgrade below.

Log in to post a comment

Jump to comment-130
max • 3 months, 2 weeks ago


This doesn't make any sense, no explanation is given as to why my code fails:

def strideCalculation(arr, lengthOfStride, windowLength):

    result = [arr[i:i+windowLength] for i in range(0, len(arr)-lengthOfStride, lengthOfStride)]  

    if len(result[-1]) < len(result[0]):


    return result

It produces exactly the desired result for the example test case when I run it locally, and it does what the problem describes, yet it fails all 5 test cases.  There really should be some sort of feedback.

Jump to comment-136
abhishek_kumar • 3 months, 2 weeks ago


Actually, there is a piece of wrong information in the Test case

It is given arr is a list.

But when you'll check its type it is actually NumPy.ndarray.  That's why you are not able to pass any of the tests. Since result[i] should be a list. But a/c to your code it is an array.

Still, You'll be able to pass only three test cases. I am unable to solve that two issues.


arr[i:i+windowLength ---------->   list([arr[i:i+windowLength])

Jump to comment-153
admin • 3 months ago


So sorry for the confusion! We have updated the variable type for this problem to specify that the first variable is a NumPy array, not a list. We have double checked all of our problems, and this issue should no longer appear for any other problem.

We appreciate the feedback, and if you ever see anything else wrong, please feel free to file a ticket via our "Feedback" feature



Jump to comment-138
abhishek_kumar • 3 months, 1 week ago


So this is one of the best examples to understand how NumPy works faster than the list.

-> Numpy uses contiguous memory.


import numpy as np
def strideCalculation(arr, lengthOfStride, windowLength):
    arr = np.asarray(arr, dtype=np.int32)
    shape_row = (len(arr)-(windowLength - lengthOfStride))//lengthOfStride
    result  = np.lib.stride_tricks.as_strided(arr, shape=(shape_row,windowLength), strides=(lengthOfStride*4,4))
    result = result.tolist()
    return result


All test cases are passing but I am hoping for a better solution than this one as it seems to me in the hard category.

For a detailed explanation about Numpy strides, I followed this great article.

Advanced NumPy: Master stride tricks with 25 illustrated exercises

Jump to comment-194
mvr • 1 month, 2 weeks ago


Might also think about mentioning library versions. For example, numpy introduced `striding_window_view` as an alternative to the unsafe `as_strided` in 1.20, which provides a very neat solution to this problem.

def strideCalculation(arr, lengthOfStride, windowLength):
    return np.lib.stride_tricks.sliding_window_view(arr, windowLength)[::lengthOfStride, :].tolist()


Input Test Case

Please enter only one test case at a time
numpy has been already imported as np (import numpy as np)