p "====================Interview questions===================="

p "1. what is singleton method?"

p "2. class_eval vs instance_eval"

p "3. cluster vs non-cluster index"

p "4. locking strategy(optimistic/pesimistic)"

p "5. how will you prevent the deadlock?"

p "6. stub vs mock"

p "7. let vs let!"

p "8. eager load vs preload ?"

p "9. SOLID principles"

p "10. what is OOPS polymorphism property ?"

p "11. Rack ?"

p "12. CORS"

p "============================================================"

class Test
end

Test.class_eval do
    def first_name
        puts "first_name"
    end
end


Test.instance_eval do
    def last_name
        puts "last_name"
    end
end

test = Test.new

test.instance_eval do
    def phone_number
        p 83792738
    end
end

p Test.new.first_name
p test.phone_number
p Test.last_name

p "====================Wrong problem===================="


arr = [1000, 3,4,6,2,88,55,66,77,33,234,999,787,44,937,998]

def second_largest_number(arr)
    max_element = arr.max
    second_largest = 0
    arr.each do |i|
        if second_largest > i && second_largest < max_element
            return second_largest
        end
        second_largest = i
    end
end

p second_largest_number(arr)

p "====================Solution===================="

arr = [1000, 3,4,6,2,88,55,66,77,33,234,999,787,44,937,998]

def print2largest(arr)
  largest = -Float::INFINITY  # Start with the smallest possible value
  second_largest = -Float::INFINITY  # Same for the second largest

  arr.each do |num|  # Loop through each element in the array
    if num > largest
      second_largest = largest  # Update second largest before largest
      largest = num  # Update the largest to the current number
    elsif num > second_largest && num != largest
      second_largest = num  # Update second largest only if it's not the same as largest
    end
  end

  second_largest == -Float::INFINITY ? -1 : second_largest  # If second largest wasn't updated, return -1
end

p print2largest(arr)







Embed on website

To embed this project on your website, copy the following code and paste it into your website's HTML: