Posts

Roman to integer best leetcode solution || interview || placement

 class Solution:     def romanToInt(self, s: str) -> int:         ans=0         d={'I':1,'V':5,'X':10,'L':50,'C':100,'D':500,'M':1000}         for i in range(0,len(s)-1):             if (d[s[i]]<d[s[i+1]]):                 ans-=d[s[i]]             else:                 ans+=d[s[i]]         ans+=d[s[-1]]                      return ans

Palindrome Number one line solution || Leetcode || Codecheff || hackerramk

 class Solution:     def isPalindrome(self, x: int) -> bool:         return x>=0 and str(x)[::-1]==str(x)

Merge two sorted array and find median

 class Solution:     def findMedianSortedArrays(self, nums1: List[int], nums2: List[int]) -> float:         res=0         nums=nums1+nums2         nums=sorted(nums)         if len(nums)%2!=0:             return nums[len(nums)//2]         else:                          res=sum(nums[(len(nums)//2)-1:(len(nums)//2)+1])/2             return res

Container with most water solution

 class Solution:     def maxArea(self, height: List[int]) -> int:         l=0         r=len(height)-1         area=0         for i in range(len(height)):             x=height[l]             y=height[r]             area=max(area,(r-l)*min(x,y))             if height[l]<height[r]:                 l+=1             else:                 r-=1         return area

Plus one Easy Python solution

 class Solution:     def plusOne(self, digits: List[int]) -> List[int]:         s=''         for i in digits:             s+=str(i)         s=int(s)+1         digits.clear()         digits=[int(i) for i in str(s)]         return digits

Search insert position in python using binary search

 class Solution:     def searchInsert(self, nums: List[int], target: int) -> int:         res=0         l=0         r=len(nums)         while(l<r):             m=(l+r)//2             if nums[m]==target:                 res=m                 break             elif target>nums[l]:                 l+=1                 res=l             else:                 r-=1         return res

Remove element easy leetcode solution

 class Solution:     def removeElement(self, nums: List[int], val: int) -> int:         k=0         j=0         for i in range(len(nums)):             if val!=nums[i]:                 k+=1                 nums[j]=nums[i]                 j+=1                          return k

Left rotation in array interview preparation

  #!/bin/python3 import   math import   os import   random import   re import   sys # # Complete the 'rotLeft' function below. # # The function is expected to return an INTEGER_ARRAY. # The function accepts following parameters: #  1. INTEGER_ARRAY a #  2. INTEGER d # def   rotLeft ( a ,   d ):      # Write your code here      temp = []      i = 0      while   i < d :          temp . append ( a [ i ])          i = i + 1      i = 0      while   d < len ( a ):          a [ i ] = a [ d ]          i = i + 1          d = d + 1      a [:] = a [: i ] + temp        return   a   if  __ name__  ==  '__main__' :      fptr  =  open ( os . environ [ 'OUTPUT_PATH' ],   'w' )      first_multiple_input  =  input () . rstrip () . split ()      n  =  int ( first_multiple_input [ 0 ])      d  =  int ( first_multiple_input [ 1 ])      a  =  list ( map ( int ,   input () . rstrip () . split ()))      result  =  rotLeft ( a ,   d )      fptr . write ( ' ' . join

TCS digital coding problems and solutions 2022-2023

TCS Digital Coding Problem -2   Problem Description -:   Given two non-negative integers n1 and n2 For example: Suppose n1=11 and n2=15. There is the number 11, which has repeated digits, but 12, 13, 14 and 15 have no repeated digits. So, the output is 4. Example1: Input: 11 — Vlaue of n1 15 — value of n2 Output: 4 Example 2: Input: 101 — value of n1 200 — value of n2 Output: 72 Python - n1=int(input()) n2=int(input()) l=[] c=0 for i in range(n1,n2+1):     while i!=0:         r=i%10         l.append(r)         i=i//10     if not(len(set(l))<len(l)):         c+=1     l.clear()      print(c)     

TCS digital coding question with solution 2022-2023

TCS Digital Coding problem -1   Problem Description -:  Given an array Arr[ ] of N integers and a positive integer K. The task is to cyclically rotate the array clockwise by K. Note  : Keep the first of the array unaltered.   Example 1: 5  —Value of N {10, 20, 30, 40, 50}  —Element of Arr[ ] 2  —–Value of K Output : 40 50 10 20 30 Example 2: 4  —Value of N {10, 20, 30, 40}  —Element of Arr[] 1  —–Value of K Output : 40 10 20 30  Python - n=int(input()) arr=[int(i) for i in input().split()] k=int(input()) rfirst=arr[0:n-k] rsecond=arr[n-k:] print(rsecond+rfirst)