Nitin Agrawal
Contact -
  • Home
  • Interviews
    • Secret Receipe
    • InterviewFacts
    • Resume Thoughts
    • Daily Coding Problems
    • BigShyft
    • Companies
    • Interviews Theory
  • Programming Languages
    • Java Script >
      • Tutorials
      • Code Snippets
    • Reactive Programming >
      • Code Snippets
    • R
    • DataStructures >
      • LeetCode Problems >
        • Problem10
        • Problem300
      • AnagramsSet
    • Core Java >
      • Codility
      • Program Arguments OR VM arguments & Environment variables
      • Java Releases >
        • Java8 >
          • Performance
          • NasHorn
          • WordCount
          • Thoughts
        • Java9 >
          • ServiceLoaders
          • Lambdas
          • List Of Objects
          • Code Snippets
        • Java14 >
          • Teeing
          • Pattern
          • Semaphores
        • Java17 >
          • Switches
          • FunctionalStreams
          • Predicate
          • Consumer_Supplier
          • Collectors in Java
        • Java21 >
          • Un-named Class
          • Virtual Threads
          • Structured Concurrency
      • Threading >
        • ThreadsOrder
        • ProducerConsumer
        • Finalizer
        • RaceCondition
        • Executors
        • Future Or CompletableFuture
      • Important Points
      • Immutability
      • Dictionary
      • Sample Code Part 1 >
        • PatternLength
        • Serialization >
          • Kryo2
          • JAXB/XSD
          • XStream
        • MongoDB
        • Strings >
          • Reverse the String
          • Reverse the String in n/2 complexity
          • StringEditor
          • Reversing String
          • String Puzzle
          • Knuth Morris Pratt
          • Unique characters
          • Top N most occurring characters
          • Longest Common Subsequence
          • Longest Common Substring
        • New methods in Collections
        • MethodReferences
        • Complex Objects Comparator >
          • Performance
        • NIO >
          • NIO 2nd Sample
        • Date Converter
        • Minimum cost path
        • Find File
      • URL Validator
    • Julia
    • Python >
      • Decorators
      • String Formatting
      • Generators_Threads
      • JustLikeThat
    • Go >
      • Tutorial
      • CodeSnippet
      • Go Routine_Channel
      • Suggestions
    • Methodologies & Design Patterns >
      • Design Principles
      • Design Patterns >
        • TemplatePattern
        • Adapter Design Pattern
        • Proxy
        • Lazy Initialization
        • CombinatorPattern
        • Singleton >
          • Singletons
        • Strategy
  • Frameworks
    • Apache Velocity
    • React Library >
      • Tutorial
    • Spring >
      • Spring Boot >
        • CustomProperties
        • ExceptionHandling
        • Custom Beans
        • Issues
      • Quick View
    • Rest WebServices >
      • Interviews
      • Swagger
    • Cloudera BigData >
      • Ques_Ans
      • Hive
      • Apache Spark >
        • ApacheSpark Installation
        • SparkCode
        • Sample1
        • DataFrames
        • RDDs
        • SparkStreaming
        • SparkFiles
    • Integration >
      • Apache Camel
    • Testing Frameworks >
      • JUnit >
        • JUnit Runners
      • EasyMock
      • Mockito >
        • Page 2
      • TestNG
    • Blockchain >
      • Ethereum Smart Contract
      • Blockchain Java Example
    • Microservices >
      • Messaging Formats
      • Design Patterns
    • AWS >
      • Honeycode
    • Dockers >
      • GitBash
      • Issues
      • Kubernetes
  • Databases
    • MySql
    • Oracle >
      • Interview1
      • SQL Queries
    • Elastic Search
  • Random issues
    • TOAD issue
    • Architect's suggestions
  • Your Views

Interview: Given array/list of numbers, separate out even and odd numbers.

4/22/2022

0 Comments

 
 First check if it is an Array or List of numbers/integers, as you are supposed to provide the
​code for that.
 Check if you are supposed to solve this problem using Functional way or Imperative style.

Though chances are very high, as you are supposed to write the code for such a simple
problem, then the interviewer is looking your functional way.

 It is to be written using Functional way & if interviewer says you can take anything List or
Array. Then go with List & solution will be like shown below-
Using List

    
 Now if interviewer says that it is array of numbers, then surely that interviewer
has recently worked on such scenario or seen on internet while preparing for the
nterviews.

 As if you are supposed to give the sample using an array, then be careful while declaring
​such array. Why?
 If you declare your array as int[] array, then it needs to be handled in different way.
 So you will need to convert this array to Stream.
 We have 2 ways for this-
 a) Use Arrays.stream()
 b) Use Stream.of();

What's the issue then?
Lets have such array like : int[] premNums = {1,2,3,4,5,6,7,8,9,10};
 Using (a) : IntStream intStream = Arrays.stream(premNums);
Here we get IntStream, & looking this seems no issue. I also thought the same but I didn't
get the way to solve the problem using this IntStream, if anyone knows please comment.

 Using (b) : Stream<int[]> arrayStream = Stream.of(premNums);
Here we see the problem now directly, as we need the stream of numbers but here we
are getting stream of int[].
 Here it becomes bit tricky to get this stream like the way we need.
 Below is the way & note the use of boxed() in flatMap()
Using array of primitive integers

    
 Now if we declare our array of numbers as : Integer[] nums = {1,2,3,4,5,6,7,8,9,10};
 So here this makes the life bit easy & like List of numbers. Why?
Because now if we convert this array to Stream using either (a) or (b) as suggested above, then in both the cases we get stream of Integer, not stream of int[], like shown in below code-
Using array of objects or wrapper types

    
0 Comments



Leave a Reply.

    Author

    Nitin Agrawal

    Archives

    July 2022
    April 2022

    Categories

    All

Powered by Create your own unique website with customizable templates.