Posts

Showing posts from December, 2022

Protoype bean scope - real world usage

  Protoype scope illustration using Token Generator import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.context.annotation.Scope; import org.springframework.stereotype.Component; @SpringBootApplication public class PrototypeScopeExample {   public static void main(String[] args) {     SpringApplication.run(PrototypeScopeExample.class, args);   } } @Component @Scope("prototype") class PrototypeBean {   private String message;   public String getMessage() {     return message;   }   public void setMessage(String message) {     this.message = message;   } } @Component class ClientBean {   @Autowired   private PrototypeBean prototypeBean;   public void printMessage() {     prototypeBean.setMessage("Hello, World!");     System.out.pr...

height checker better option

 Leetcode question of height checker given an array of integers, not in ascending order, find mismatch nodes count https://leetcode.com/problems/height-checker/description/ Typical solution is regular array sort and compare - O(n logn) Counting sort or use approach of buckets then scan again - O(n) counting sort approach - https://leetcode.com/problems/height-checker/solutions/299910/java-o-n-no-sort/ bucket based solution - https://leetcode.com/problems/height-checker/solutions/299910/java-o-n-no-sort/