Coder Social home page Coder Social logo

desainis / practice-problems Goto Github PK

View Code? Open in Web Editor NEW
0.0 1.0 2.0 11 KB

License: MIT License

Python 100.00%
problem-statement interview interview-questions interview-practice interview-preparation interview-test interviews interviewing interview-prep interviewbit interviewbit-solutions

practice-problems's Introduction

Interviewing Library

This repository contains problems and solutions to common interview questions. Please look at the template.py for problem statements only. The solution.py contains a workable solution.

For example, take the problem statement below:

'''
Given a sorted array, A, with possibly duplicated elements, find the indices of the first and last occurrences of a target element, x. Return -1 if the target is not found.

Example:
Input: A = [1,3,3,5,7,8,9,9,9,15], target = 9
Output: [6,8]

Input: A = [100, 150, 150, 153], target = 150
Output: [1,2]

Input: A = [1,2,3,4,5,6,10], target = 9
Output: [-1, -1]
'''

class Solution: 
  def getRange(self, arr, target):
    # Fill this in.
  
# Test program 
arr = [1, 2, 2, 2, 2, 3, 4, 7, 8, 8] 
x = 2
print(Solution().getRange(arr, x))
# [1, 4]
  • An example solution.py
'''
Given a sorted array, A, with possibly duplicated elements, find the indices of the first and last occurrences of a target element, x. Return -1 if the target is not found.

Example:
Input: A = [1,3,3,5,7,8,9,9,9,15], target = 9
Output: [6,8]

Input: A = [100, 150, 150, 153], target = 150
Output: [1,2]

Input: A = [1,2,3,4,5,6,10], target = 9
Output: [-1, -1]
'''

class Solution: 
  def getRange(self, arr, target):
    start = -1
    end = -1
    for i in range(len(arr)):
      if arr[i] == target:
        start = i
        break
    
    for j in range(len(arr) - 1, -1, -1):
      if arr[j] == target:
        end = j
        break
    
    return [start, end]
    # Fill this in.
  
# Test program 
arr = [1, 2, 2, 2, 2, 3, 4, 7, 8, 8] 
x = 2
print(Solution().getRange(arr, x))
#[1, 4]

arr = [1,3,3,5,7,8,9,9,9,15] 
x = 9
print(Solution().getRange(arr, x))
# [6, 8]

arr = [100, 150, 150, 153] 
x = 150
print(Solution().getRange(arr, x))
# [1, 2]

arr = [1,2,3,4,5,6,10] 
x = 9
print(Solution().getRange(arr, x))
# [-1, -1]
  • Enjoy problem solving!

practice-problems's People

Contributors

desainis avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.