Input Format
The first line contains , the number of lines in the XML document.
The next  lines follow containing the XML document.
Output Format
Output a single line, the integer value of the maximum level of nesting in the XML document.

hackerrank xml 2 find the maximum depth problem solution in python programming

Problem solution in Python 3 programming language.

import xml.etree.ElementTree as etree

maxdepth = 0
def depth(elem, level):
    global maxdepth
    level += 1
    if (maxdepth < level):
        maxdepth = level
    for child in elem:
        depth(child, level)
        
if __name__ == '__main__':
    n = int(input())
    xml = ""
    for i in range(n):
        xml =  xml + input() + "\n"
    tree = etree.ElementTree(etree.fromstring(xml))
    depth(tree.getroot(), -1)
    print(maxdepth)

Second solution

import xml.etree.ElementTree as etree
global maxdepth
maxdepth = -1
def depth(elem, level):
    global maxdepth
    if (level == maxdepth):
        maxdepth += 1
    for child in elem:
        depth(child, level + 1)
if __name__ == '__main__':
    n = int(input())
    xml = ""
    for i in range(n):
        xml =  xml + input() + "\n"
    tree = etree.ElementTree(etree.fromstring(xml))
    depth(tree.getroot(), -1)
    print(maxdepth)

Problem solution in Python 2 programming language.

import xml.etree.ElementTree as etree

maxdepth = 0
def depth(elem, level):
    global maxdepth
    level += 1
    if (maxdepth < level):
        maxdepth = level
    for child in elem:
        depth(child, level)
        
if __name__ == '__main__':
    n = int(raw_input())
    xml = ""
    for i in range(n):
        xml =  xml + raw_input() + "\n"
    tree = etree.ElementTree(etree.fromstring(xml))
    depth(tree.getroot(), -1)
    print maxdepth