
I am an undergraduate computer science student from NIIT University working as a freelancer seeking work as Software Engineer. Where I can apply my experience and knowledge to the fullest extent at the same time improve my skills.
Search for a command to run...

I am an undergraduate computer science student from NIIT University working as a freelancer seeking work as Software Engineer. Where I can apply my experience and knowledge to the fullest extent at the same time improve my skills.
No comments yet. Be the first to comment.
If you're seeking for free coding activities to help you prepare for your next job interview, these algorithms in blogs are regularly used in job interviews.
We all know that for any interview point of view the graph data structures are very much important. Because in most of the cases we end up having graph-based data structures in real-life problems. Social Graph APIs such as Facebook's Graph API, Recom...

With AWS CLI commands and Google Authentication App

Every one of us, at some point, would need to deploy a static website. It might be because the page is just a portfolio or a single-page JavaScript application that would not need heavy frameworks. So most of us have to host a static website, It migh...

Sliding Window

Given an array containing 0s, 1s and 2s, sort the array in place. You should treat numbers of the array as objects, hence, we can’t count 0s, 1s, and 2s to recreate the array.
The flag of the Netherlands consists of three colours: red, white and blue; and since our input array also consists of three different numbers that are why it is called Dutch National Flag problem.
Example 1:
Input: [1, 0, 2, 1, 0]
Output: [0, 0, 1, 1, 2]
Example 2:
Input: [2, 2, 0, 1, 2, 0]
Output: [0, 0, 1, 2, 2, 2,]
So the idea is to solve, We can use a Two Pointers approach while iterating through the array. Let’s say the two pointers are called low and high which are pointing to the first and the last element of the array respectively. So while iterating, we will move all 0s before low and all 2s after high so that in the end, all 1s will be between low and high.
def dutch_flag(lst):
lower_pointer = 0
high_pointer = len(lst) - 1
iteration_pointer = 0
while iteration_pointer <= high_pointer:
if lst[iteration_pointer] == 1:
iteration_pointer += 1
elif lst[iteration_pointer] == 0:
lst[lower_pointer], lst[iteration_pointer] = lst[iteration_pointer], lst[lower_pointer]
lower_pointer += 1
iteration_pointer += 1
elif lst[iteration_pointer] == 2:
lst[high_pointer], lst[iteration_pointer] = lst[iteration_pointer], lst[high_pointer]
high_pointer -= 1
return lst
def main():
arr = [1, 0, 2, 1, 0]
dutch_flag(arr)
print(arr)
arr = [2, 2, 0, 1, 2, 0]
dutch_flag(arr)
print(arr)
The final out put is
[0, 0, 1, 1, 2] [0, 0, 1, 2, 2, 2]
Two