Rest API Design guidelines and best practices

When designing an api you should think about

  • naming
  • Proper http verbs and response codes for different CRUD operations
  • Think about versioning.

General guidelines to keep in mind:

-> Always prefer to use lowercase characters in uri’s. A forward slash should be used to indicate a hierarchical relationship. Also a trailing forward slash should be not be included. Hyphens (-) can be used for better readability but underscores ( _ ) should not be used.

Note: File extensions like ‘json’ or ‘xml’ should not be used to indicate a format preference. You should look at the ‘Content-Type’ header to determine how to process the body’s content.

-> The uri path should convey the resource model with each segment path identifying an addressable resource.

-> The actual words in the path should either be a singular ,plural  noun or a verb.

Note: Do not use CRUD function names in the uri’s. You can use the query component of the uri for pagination.


-> GET and POST must not be used to tunnel other request methods

->  POST must be used to execute controllers

-> HEAD should be used to retrieve response headers

Response Codes:

200 (OK) Non-specific Success.Must not be used to communicate errors in response body.
201 (OK) Successful resource creation.
204 (No content) Used when response body is intentionally empty
301(“Moved Permanently”) Should be used to relocate resources
302 (“Found”) Should not be used
303 (“See Other”) Should be used to refer the client to a different URI
307 (“Temporary Redirect”) Should be used to tell clients to resubmit the request to another URI
400 (“Bad Request”) Used to indicate nonspecific failure
401 (“Unauthorized”) Used when there is a problem with the client’s credentials
403 (“Forbidden”) Used to forbid access regardless of authorization state
404 (“Not Found”) Used when a client’s URI cannot be mapped to a resource
405 (“Method Not Allowed”) Used when the HTTP method is not supported
406 (“Not Acceptable”) Used when the requested media type cannot be served
415 (“Unsupported Media Type”) Used when the media type of a request’s payload cannot be processed
500 (“Internal Server Error”) Used to indicate API malfunction

HTTP Headers:

-> Content-Type, Content-Length should be used

Note: Cache-Control, Expires, and Date response headers should be used to encourage caching.Expiration caching headers should be used with 200 (“OK”) responses

-> Custom HTTP headers must not be used to change the behavior of HTTP methods

Error Representation

A consistent form should be used to represent errors and error responses.Consistent error types should be used for common error conditions.


Bitwise AND between a range of integers

Problem Statement

You will be given two integers A and B. You are required to compute the bitwise AND amongst all natural numbers lying between A and B, both inclusive.

Since & is only 1 when both are 1 and when calculating in a range if the range ends do not both have 1 the rest of the whole binary is 0

The only bits that will be 1 will be bits that are common to the upper bits of A and B. Everything else will have at least one instance of a 0 in that range. So just start from the high order bit downwards. Output the matching bits. As soon as you hit a disagreement between the binaries of A and B (which will be 0 in A and 1 in B) output zeros until you get to the length of B.

def bitAndWholeArray(arr):
string1 = bin(arr[0])
string2 = bin(arr[1])
index_count = 0
res = ‘0b’
for index,i in enumerate(string1):
if index == 0 or index == 1:
if i == string2[index]:
res = res + i
index_count = index

for _ in range(index_count,len(string2)):
res = res + ‘0’

return int(res,2)
n = int(raw_input())
for _ in range(n):
arr = map(int,raw_input().split(‘ ‘))
print bitAndWholeArray(arr)

Find the majority element in an array


1) take the maj element as x

2) Loop through the array and

    – Increment counter if x

    – Decrement counter if not x

    – If counter 0 the maj element is the new element


def checkMajorityElement(arr):

    x = arr[0]

    count = 1

    for i,index in enumerate(arr):


        if index == 1:



        if i == x:

            count = count +  1


            count = count – 1



    return x


Convert to 24 hour time format


Created on Jun 17, 2015

@author: ishaansutaria


def convert24HourFormat(time_str):

    time_str = time_str.upper()

    time_24 =

    if ‘PM’ in time_str and ’12’ in time_str:

        time_24 = time_str

    elif ‘PM’ in time_str:

        temp_list = time_str.split(‘:’)

        time_24 = time_24 + str(int(temp_list[0]) + 12)


        time_24 = time_24 + ‘:’ + temp_list[1] + ‘:’ + temp_list[2]

    elif ‘AM’ in time_str and ’12’ in time_str:

        temp_list = time_str.split(‘:’)

        time_24 = time_24 + ‘0’ + str(int(temp_list[0]) – 12)


        time_24 = time_24 + ‘:’ + temp_list[1] + ‘:’ + temp_list[2]


        time_24 = time_str


    print time_24[:-2]



Print a staircase

Example Output:







def staircase(n):

    for j in range(n-1,-1,-1):

        s =

        for i in xrange(n):

            if i >= j:

                s = s + ‘#’


                s = s + ‘ ‘

        print s


Calculate the difference between two diagonals of a square matrix


Created on Jun 15, 2015

@author: ishaansutaria



(Given N * N matrix)

1 2 3 4

1 2 3 4

1 2 3 4

1 2 3 4

1)The first diagnal is add the element whenever the row and coloum is the same

2) The second diagonal is s


def findDiagonalDifference(n,list1):

    diag_sum1 = 0

    diag_sum2 = 0

    n = n – 1

    for row_index,row in enumerate(list1):

        for col_index,col in enumerate(row):

            if col_index == row_index:

                diag_sum1 += col


            if col_index == n:

                diag_sum2 += col

                n = n – 1


    return abs(diag_sum1 – diag_sum2)


print findDiagonalDifference(4,[[1,2,3,4],[1,2,3,4],[1,2,3,4],[1,2,3,4]])