Nitin Agrawal
Contact -
  • Home
  • Interviews
    • InterviewFacts
    • Resume Thoughts
    • Daily Coding Problems
    • BigShyft
    • Companies >
      • InvestmentBanks >
        • ECS
        • Bank Of America
        • WesternUnion
        • WellsFargo
      • ProductBasedCompanies >
        • Google
        • Microsoft >
          • Microsoft
        • CA Technologies
        • Model N India
        • Verizon Media
        • Oracle & GoJek
        • IVY Computec
        • Nvidia
        • Samsung
        • ClearWaterAnalytics
        • ADP
        • ServiceNow
        • Pubmatic
        • Expedia
        • Amphora
        • CDK Global
        • Delphix
        • Fractal Enterprises LLP
        • CDK Global
        • Tide-Banking
        • Epic
        • Sincro-Pune
        • Whiz.AI
        • ChargePoint
      • ServiceBasedCompanies >
        • Sapient
        • Altimetrik
        • ASG World Wide Pvt Ltd
        • Paraxel International & Pramati Technologies Pvt Ltd
        • MitraTech
        • Intelizest Coding Round
    • Interviews Theory
  • Programming Languages
    • Java Script >
      • Tutorials
      • Code Snippets
    • Reactive Programming >
      • Code Snippets
    • R
    • DataStructures >
      • LeetCode Problems
      • AnagramsSet
    • Core Java >
      • Codility
      • Threading >
        • ThreadsOrder
        • ProducerConsumer
        • Finalizer
        • RaceCondition
        • Executors
        • Future Or CompletableFuture
      • Important Points
      • Immutability
      • Dictionary
      • URL Validator
    • Julia
    • Python >
      • Decorators
      • String Formatting
      • Generators_Threads
      • JustLikeThat
    • Go >
      • Tutorial
      • CodeSnippet
      • Go Routine_Channel
      • Suggestions
    • Methodologies & Design Patterns >
      • Design Principles
      • Design Patterns >
        • Decorator
        • Proxy
        • Lazy Initialization
        • CombinatorPattern
        • RequestChaining
        • Singleton >
          • Singletons
  • Frameworks
    • Apache Velocity
    • Spring >
      • Spring Boot >
        • CustomProperties
        • ExceptionHandling
        • 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
  • Databases
    • MySql
    • Oracle >
      • Interview1
      • SQL Queries
    • Elastic Search >
      • StudySources
  • 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

    April 2022

    Categories

    All

Powered by Create your own unique website with customizable templates.