DocsApp Interview Question

Hacker earth two algorithms question