Fail Fast & Self Healing Using Circuit Breaker

What is Circuit Breaker?

Circuit Breaker is a pattern to monitor the failures and it keeps the count of failure, after reaching the threshold of failure. it stops sending requests to the external server till timeout.

State of the Circuit Breaker

Circuit Breaker has three states:

Closed: It is a normal state. Every request going through the services.

Open: When shit started, If External server is started to failing the requests then the circuit breaker comes in open state once the failure threshold reached. No requests go to the external services. Simply fail fast.

Half-Open: After the timeout, the Circuit breaker went to the half-open state, now requests go to the services and if any requests failed then still state moved to open state otherwise reset to the closed state. Simply automatic healing. 

How it benefits?

It prevents cascading failures and keeps the system responsive.

  • We can set a default response until Circuit Breaker is in open state.
  • Other functionality & API should work.
  • If we know the other system is down then no need to send requests & wait for a response.
  • Automatically check the system status in the half-open state.


Further Reading:

  1. Martin Flower blog:
  2. Netflix blog:
  3. Netflix Open Source Hystrix:

Easily, Delete your tweets from your Twitter account using python

If you want to delete your tweets from your twitter account then it is very easy to do using python.


Please follow these steps to delete your twitter’s tweet status:


1: Install tweepy python library either using pip or conda python package manager.

2. Get your Consumer API key and Consumer API secret key from your twitter app.

3. Get your API access token & API secret token from your twitter app.

Python code:

In-Place QuickSort

Create an in-place version of Quicksort. You need to follow Lomuto Partitioning method.

Instead of copying the array into multiple sub-arrays, use indices to keep track of the different sub-arrays. You can pass the indices to a modified partition method. The partition method should partition the sub-array and then return the index location where the pivot gets placed, so you can then call partition on each side of the pivot.

Always select the last element in the ‘sub-array’ as a pivot.
Partition the left side and then the right side of the sub-array.
Print out the whole array at the end of every partitioning method.
An array of length 11 or less will be considered sorted, and there is no need to sort or to print them.
Since you cannot just create new sub-arrays for the elements, partition method will need to use another trick to keep track of which elements are greater and which are lower than the pivot.

The In-place Trick

If an element is lower than the pivot, you should swap it with a larger element on the left-side of the sub-array.
Greater elements should remain where they are.
At the end of the partitioning method, the pivot should be swapped with the first element of the right partition, consisting of all larger elements, of the sub-array.
To ensure that you don’t swap a small element with another small element, use an index to keep track of the small elements that have already been swapped into their “place”.

Input Format
There will be two lines of input:

nn – the size of the array
arar – the nn numbers of the array
Output Format
Print the entire array on a new line at the end of every partitioning method.

There are no duplicate numbers.

Sample Input

1 3 9 8 2 7 5
Sample Output

1 3 2 5 9 7 8
1 2 3 5 9 7 8
1 2 3 5 7 8 9
5 is initally selected as the pivot, and the array is partitioned as shown in the diagram. The left side is partitioned next with 2 as the pivot. Finally, the right side is partitioned with 8 as the pivot. The entire array is now sorted.


Maximum AND

Given two numbers A and B. Find the value of pair (P,Q) such that A <= P < Q <= B value of P AND Q is maximum where AND is a binary operator. Refer to this link for more information about AND operator :

First line of input contains number of test cases T. Each test case contains two numbers A and B.

Output: For each test case print the value of maximum AND.

1<= A < B <=1018

Sample Input(Plaintext Link)
2 3
4 8

Sample Output(Plaintext Link)

Hackerearth Coding Challenge

#python sollution
T = int(raw_input())

for i in range(T):
	x, y = map(int,raw_input().split(" "))
	if y % 2 == 0:
		if x < ( y - 1 ) :
			y = y - 1
			x = y - 1
	elif y % 2 != 0 and x < y:
		x = y - 1
	print x & y

Time Conversion

Problem Statement

Given a time in AM/PM format, convert it to military (24-hour) time.

Note: Midnight is 12:00:00AM on a 12-hour clock and 00:00:00 on a 24-hour clock. Noon is 12:00:00PM on a 12-hour clock and 12:00:00 on a 24-hour clock.

Input Format

A time in 12-hour clock format (i.e.: hh:mm:ssAM or hh:mm:ssPM), where 01≤hh≤12.

Output Format

Convert and print the given time in 24-hour format, where 00≤hh≤23.

Sample Input

Sample Output


7 PM is after noon, so you need to add 12 hours to it during conversion. 12 + 7 = 19. Minutes and seconds do not change in 12-24 hour time conversions, so the answer is 19:05:45.

Copyright © 2015 HackerRank.
All Rights Reserved


hh, mm, ss = raw_input().split(":")
h = int(hh)
T = ss[2:]
if T == 'PM' and h < 12:
    print str(12 + h) + ":" + mm + ":"+ss[0:2]
elif T == 'AM' and h == 12:
    print '00'+":"+mm+":"+ss[0:2]
    print hh+":"+mm+":"+ss[0:2]

Diagonal Difference

Problem Statement

Given a square matrix of size N×N, calculate the absolute difference between the sums of its diagonals.

Input Format

The first line contains a single integer, N. The next N lines denote the matrix’s rows, with each line containing N space-separated integers describing the columns.

Output Format

Print the absolute difference between the two sums of the matrix’s diagonals as a single integer.

Sample Input

11 2 4
4 5 6
10 8 -12

Sample Output


The primary diagonal is:


Sum across the primary diagonal: 11 + 5 – 12 = 4

The secondary diagonal is:


Sum across the secondary diagonal: 4 + 5 + 10 = 19
Difference: |4 – 19| = 15

Copyright © 2015 HackerRank.
All Rights Reserved

#Python solution
N = int(raw_input())
sum1 = 0
sum2 = 0
for i in range(N):
    list1 = map(int, raw_input().split(" "))
    sum1 = sum1 + list1[i]
    sum2 = sum2 + list1[N-(i+1)]
print abs(sum1-sum2)

Save Quantumland

Problem Statement

In Quantumland, there are n cities numbered from 1 to n. Here, ci denotes the ith city. There are n−1 roads in Quantumland. Here, ci and ci+1 have a bidirectional road between them for each i<n.

There is a rumor that Flatland is going to attack Quantumland, and the queen wants to keep her land safe. The road between ci and ci+1 is safe if there is a guard in ci or ci+1.

The queen has already placed a few guards in some of the cities, but she is not sure if they are enough to keep the roads safe. She wants to know the minimum number of new guards she needs to hire.

Input Format

The first line will contain an integer t: denoting the number of testcases.

For each testcase, there will be 2 lines.

The first line will contain an integer n.
In the next line, there will be n integers b1,b2…bn.

If bi=1, that means there is a guard in city ci.
If bi=0, that means there are no guards in city ci.


Output Format

Print the minimum number of new guards that need to be hired to make Quantumland safe.

Sample Input

1 1 0 1 0
0 0 1 0 0
Sample Output 1

The following figure represents the first testcase. The cities with guards are marked in blue.


All the roads are safe. No more guards are needed. The output is 0.

The next figure represents the second testcase. The old guards are marked in blue, and the new guards are marked in yellow.


Note: There can be more than one way to make Quantumland safe using the minimum number of new guards. In the second testcase, we could also place new guards in city 1 and city 4 instead of city 2 and city 4.
Contest On Hackerrank


import sys

t = int(raw_input().strip())
for a0 in xrange(t):
    n = int(raw_input().strip())
    count  = 0
    a = map(int,raw_input().strip().split(' '))
    for i in range(len(a) - 1 ) :
        if a[i] != 1 and a[i+1] != 1:
            a[i+1] = 1
            count = count + 1
    print count