SlideShare a Scribd company logo
Coding Test Review 3
LeetCode – Power of Two (Easy)
𝑂(log 𝑛) 𝑠𝑜𝑙𝑢𝑡𝑖𝑜𝑛
class Solution:
def isPowerOfTwo(self, n: int) -> bool:
if n <= 0:
return False
while n>1:
if n%2 == 0:
n = n/2
else:
return False
return True
LeetCode – Valid Parentheses (Easy)
𝑂(𝑛) 𝑠𝑜𝑙𝑢𝑡𝑖𝑜𝑛
class Solution:
def isValid(self, s: str) -> bool:
new_s = ' '
for text in s:
if new_s[-1] == '{' and text == '}':
new_s = new_s[:-1]
continue
elif new_s[-1] == '(' and text == ')':
new_s = new_s[:-1]
continue
elif new_s[-1] == '[' and text == ']':
new_s = new_s[:-1]
continue
elif new_s[-1] == '{' and (text == ')' or text == ']'):
return False
elif new_s[-1] == '(' and (text == ']' or text == '}'):
return False
elif new_s[-1] == '[' and (text == ')' or text == '}'):
return False
new_s += text
if new_s[1:] == '':
return True
else:
return False
LeetCode – Find Minimum in Rotated Sorted Array (Medium)
𝑂(𝑛) 𝑠𝑜𝑙𝑢𝑡𝑖𝑜𝑛
class Solution:
def findMin(self, nums: List[int]) -> int:
prev = nums[0] - 1
for num in nums:
if prev > num:
return num
prev = num
return nums[0]
𝑂 log 𝑛 𝑠𝑜𝑙𝑢𝑡𝑖𝑜𝑛
class Solution:
def findMin(self, nums: List[int]) -> int:
cur = nums
while len(cur)>0:
leng = len(cur)
if leng == 1:
return cur[0]
mid = int(leng/2)
if len(cur[:mid]) > 1:
if cur[:mid][0] > cur[:mid][-1] and cur[:mid][-1] < cur[mid:][0]:
cur = cur[:mid]
elif cur[:mid][0] < cur[:mid][-1] and cur[:mid][0] < cur[mid:][-1]:
return cur[:mid][0]
else:
cur = cur[mid:]
elif len(cur[:mid]) == 0:
cur = cur[mid:]
elif len(cur[:mid]) == 1:
if cur[:mid][0] > cur[mid:][-1]:
cur = cur[mid:]
else:
return cur[:mid][0]
Thank You

More Related Content

PDF
Swift Study #3
PDF
8th alg -l7.1
DOC
String in c
PPT
PPTX
Strings in programming tutorial.
PPTX
Handling of character strings C programming
PDF
8th pre alg -l31
Swift Study #3
8th alg -l7.1
String in c
Strings in programming tutorial.
Handling of character strings C programming
8th pre alg -l31

What's hot (11)

PPTX
String in c programming
PPTX
C programming - String
PDF
Python strings
PDF
Matlab calculus
PPTX
String in python lecture (3)
PPT
Strings
PPTX
Ruby data types and objects
PPTX
Implementation Of String Functions In C
PPTX
1.5 & 1.6 regular languages &amp; regular expression
String in c programming
C programming - String
Python strings
Matlab calculus
String in python lecture (3)
Strings
Ruby data types and objects
Implementation Of String Functions In C
1.5 & 1.6 regular languages &amp; regular expression
Ad

Similar to Coding Test Review 3 (20)

PDF
Python Homework Sample
DOCX
ECE-PYTHON.docx
PDF
AI_Lab_File()[1]sachin_final (1).pdf
PPTX
1P13 Python Review Session Covering various Topics
PDF
Artificial Intelligence Practical Manual.pdf
PDF
python practicals-solution-2019-20-class-xii.pdf
PPTX
Introduction to Python Programming.pptx
PPTX
Perfect Python Homework Help
PDF
AI-Programs.pdf
DOC
Programs.doc
PDF
Amcat automata questions
PDF
Amcat automata questions
PDF
Demystifying Software Interviews
PDF
Zoho Interview Questions By Scholarhat.pdf
PDF
xii cs practicals class 12 computer science.pdf
PPT
Profiling and optimization
DOCX
pythonexam.docx
PDF
Coding test review
PDF
Coding test review2
DOCX
Basic python laboratoty_ PSPP Manual .docx
Python Homework Sample
ECE-PYTHON.docx
AI_Lab_File()[1]sachin_final (1).pdf
1P13 Python Review Session Covering various Topics
Artificial Intelligence Practical Manual.pdf
python practicals-solution-2019-20-class-xii.pdf
Introduction to Python Programming.pptx
Perfect Python Homework Help
AI-Programs.pdf
Programs.doc
Amcat automata questions
Amcat automata questions
Demystifying Software Interviews
Zoho Interview Questions By Scholarhat.pdf
xii cs practicals class 12 computer science.pdf
Profiling and optimization
pythonexam.docx
Coding test review
Coding test review2
Basic python laboratoty_ PSPP Manual .docx
Ad

More from SEMINARGROOT (20)

PDF
Metric based meta_learning
PDF
Sampling method : MCMC
PDF
Demystifying Neural Style Transfer
PDF
Towards Deep Learning Models Resistant to Adversarial Attacks.
PDF
The ways of node embedding
PDF
Graph Convolutional Network
PDF
Denoising With Frequency Domain
PDF
Bayesian Statistics
PDF
Time Series Analysis - ARMA
PDF
Differential Geometry for Machine Learning
PDF
Generative models : VAE and GAN
PDF
Effective Python
PDF
Understanding Blackbox Prediction via Influence Functions
PDF
Attention Is All You Need
PDF
Attention
PDF
WWW 2020 XAI Tutorial Review
PDF
Coding test review 2
PDF
Locality sensitive hashing
PDF
Coding Test Review1
PDF
Strong convexity on gradient descent and newton's method
Metric based meta_learning
Sampling method : MCMC
Demystifying Neural Style Transfer
Towards Deep Learning Models Resistant to Adversarial Attacks.
The ways of node embedding
Graph Convolutional Network
Denoising With Frequency Domain
Bayesian Statistics
Time Series Analysis - ARMA
Differential Geometry for Machine Learning
Generative models : VAE and GAN
Effective Python
Understanding Blackbox Prediction via Influence Functions
Attention Is All You Need
Attention
WWW 2020 XAI Tutorial Review
Coding test review 2
Locality sensitive hashing
Coding Test Review1
Strong convexity on gradient descent and newton's method

Recently uploaded (20)

PPTX
Engineering Ethics, Safety and Environment [Autosaved] (1).pptx
PPTX
CARTOGRAPHY AND GEOINFORMATION VISUALIZATION chapter1 NPTE (2).pptx
DOCX
ASol_English-Language-Literature-Set-1-27-02-2023-converted.docx
PDF
Mohammad Mahdi Farshadian CV - Prospective PhD Student 2026
PDF
July 2025 - Top 10 Read Articles in International Journal of Software Enginee...
PDF
Enhancing Cyber Defense Against Zero-Day Attacks using Ensemble Neural Networks
PDF
PRIZ Academy - 9 Windows Thinking Where to Invest Today to Win Tomorrow.pdf
PDF
Digital Logic Computer Design lecture notes
PPTX
Foundation to blockchain - A guide to Blockchain Tech
PPTX
M Tech Sem 1 Civil Engineering Environmental Sciences.pptx
PDF
TFEC-4-2020-Design-Guide-for-Timber-Roof-Trusses.pdf
PDF
R24 SURVEYING LAB MANUAL for civil enggi
PPTX
UNIT-1 - COAL BASED THERMAL POWER PLANTS
PPTX
KTU 2019 -S7-MCN 401 MODULE 2-VINAY.pptx
PDF
The CXO Playbook 2025 – Future-Ready Strategies for C-Suite Leaders Cerebrai...
PDF
Embodied AI: Ushering in the Next Era of Intelligent Systems
PPTX
Recipes for Real Time Voice AI WebRTC, SLMs and Open Source Software.pptx
PPTX
Lecture Notes Electrical Wiring System Components
PDF
SM_6th-Sem__Cse_Internet-of-Things.pdf IOT
DOCX
573137875-Attendance-Management-System-original
Engineering Ethics, Safety and Environment [Autosaved] (1).pptx
CARTOGRAPHY AND GEOINFORMATION VISUALIZATION chapter1 NPTE (2).pptx
ASol_English-Language-Literature-Set-1-27-02-2023-converted.docx
Mohammad Mahdi Farshadian CV - Prospective PhD Student 2026
July 2025 - Top 10 Read Articles in International Journal of Software Enginee...
Enhancing Cyber Defense Against Zero-Day Attacks using Ensemble Neural Networks
PRIZ Academy - 9 Windows Thinking Where to Invest Today to Win Tomorrow.pdf
Digital Logic Computer Design lecture notes
Foundation to blockchain - A guide to Blockchain Tech
M Tech Sem 1 Civil Engineering Environmental Sciences.pptx
TFEC-4-2020-Design-Guide-for-Timber-Roof-Trusses.pdf
R24 SURVEYING LAB MANUAL for civil enggi
UNIT-1 - COAL BASED THERMAL POWER PLANTS
KTU 2019 -S7-MCN 401 MODULE 2-VINAY.pptx
The CXO Playbook 2025 – Future-Ready Strategies for C-Suite Leaders Cerebrai...
Embodied AI: Ushering in the Next Era of Intelligent Systems
Recipes for Real Time Voice AI WebRTC, SLMs and Open Source Software.pptx
Lecture Notes Electrical Wiring System Components
SM_6th-Sem__Cse_Internet-of-Things.pdf IOT
573137875-Attendance-Management-System-original

Coding Test Review 3

  • 2. LeetCode – Power of Two (Easy)
  • 3. 𝑂(log 𝑛) 𝑠𝑜𝑙𝑢𝑡𝑖𝑜𝑛 class Solution: def isPowerOfTwo(self, n: int) -> bool: if n <= 0: return False while n>1: if n%2 == 0: n = n/2 else: return False return True
  • 4. LeetCode – Valid Parentheses (Easy)
  • 5. 𝑂(𝑛) 𝑠𝑜𝑙𝑢𝑡𝑖𝑜𝑛 class Solution: def isValid(self, s: str) -> bool: new_s = ' ' for text in s: if new_s[-1] == '{' and text == '}': new_s = new_s[:-1] continue elif new_s[-1] == '(' and text == ')': new_s = new_s[:-1] continue elif new_s[-1] == '[' and text == ']': new_s = new_s[:-1] continue elif new_s[-1] == '{' and (text == ')' or text == ']'): return False elif new_s[-1] == '(' and (text == ']' or text == '}'): return False elif new_s[-1] == '[' and (text == ')' or text == '}'): return False new_s += text if new_s[1:] == '': return True else: return False
  • 6. LeetCode – Find Minimum in Rotated Sorted Array (Medium)
  • 7. 𝑂(𝑛) 𝑠𝑜𝑙𝑢𝑡𝑖𝑜𝑛 class Solution: def findMin(self, nums: List[int]) -> int: prev = nums[0] - 1 for num in nums: if prev > num: return num prev = num return nums[0]
  • 8. 𝑂 log 𝑛 𝑠𝑜𝑙𝑢𝑡𝑖𝑜𝑛 class Solution: def findMin(self, nums: List[int]) -> int: cur = nums while len(cur)>0: leng = len(cur) if leng == 1: return cur[0] mid = int(leng/2) if len(cur[:mid]) > 1: if cur[:mid][0] > cur[:mid][-1] and cur[:mid][-1] < cur[mid:][0]: cur = cur[:mid] elif cur[:mid][0] < cur[:mid][-1] and cur[:mid][0] < cur[mid:][-1]: return cur[:mid][0] else: cur = cur[mid:] elif len(cur[:mid]) == 0: cur = cur[mid:] elif len(cur[:mid]) == 1: if cur[:mid][0] > cur[mid:][-1]: cur = cur[mid:] else: return cur[:mid][0]