Nitin Agrawal
Contact -
  • Home
  • Interviews
    • Secret Receipe
    • InterviewFacts
    • Resume Thoughts
    • Daily Coding Problems
    • BigShyft
    • CompanyInterviews >
      • InvestmentBanks >
        • ECS
        • Bank Of America
        • WesternUnion
        • WellsFargo
      • ProductBasedCompanies >
        • CA Technologies
        • Model N India
        • Verizon Media
        • Oracle & GoJek
        • IVY Computec
        • Nvidia
        • ClearWaterAnalytics
        • ADP
        • ServiceNow
        • Pubmatic
        • Expedia
        • Amphora
        • CDK Global
        • CDK Global
        • Epic
        • Sincro-Pune
        • Whiz.AI
        • ChargePoint
      • ServiceBasedCompanies >
        • Altimetrik
        • ASG World Wide Pvt Ltd
        • Paraxel International & Pramati Technologies Pvt Ltd
        • MitraTech
        • Intelizest Coding Round
        • EPAM
    • Interviews Theory
  • Programming Languages
    • Java Script >
      • Tutorials
      • Code Snippets
    • Reactive Programming >
      • Code Snippets
    • R
    • DataStructures >
      • LeetCode Problems
      • AnagramsSet
    • Core Java >
      • Codility
      • Program Arguments OR VM arguments & Environment variables
      • Java Releases
      • 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 >
        • TemplatePattern
        • Adapter Design Pattern
        • 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
    • Dockers >
      • GitBash
      • Issues
  • Databases
    • MySql
    • Oracle >
      • Interview1
      • SQL Queries
    • Elastic Search
  • Random issues
    • TOAD issue
    • Architect's suggestions
  • Your Views
Below question was asked in IBM, as per Daily Coding Problem article -

Given an integer, find the next permutation of it in absolute order. For example, given 48975, the next permutation would be 49578

Below are my options & don't forget to check till the last option in the end-
Picture
Obviously it is Brute-Force solution & to this the interviewer, who might have see a better solution over the internet while searching for the interview questions, will point that this is slow & can you make it better. These people are generally headless & like to show themselves more intellegent than the candidates. So never give the better approach first, always give some naive or brute-force approach to these interviewers so that they can have their ego.
So not better solution you can give like shown below. And I believe any approach during the interviews depends on what strikes you first but most interviewers don't understand this.
Picture
 To the above solution even, your interviewer doesn't stop as s/he has see some other solution. So s/he will again ask saying that can we improve the code, as it is highly possible that s/he doesn't know about the performance in it so just checking for the code improvement. But you know that its performance can be improved even.
Below is improved version with little tweaks & it works more than 10 times faster.
Picture
 Now you have seen above solutions
Did you understand these?
Did these work for your test cases?
If these work for your test cases then we are on same page & have to agree that proper test cases are not created in first attempt.
​Check below code for slow version & see the difference -
Picture
 Next is its fast version -
Picture
 Next is the fastest version -
Picture
 Above kind of solution is fine for Integers, but what if we have a very long number far greater than an integer or long & this number we will represent with String & will do the processing like above & below is that implementation -
Picture
 I hope you checked all the above options.
 Did you understand the logic?
Did you find any issue there?
 But at least you got few thoughts about how the performance is improved by little tweaks & how we move to better solution gradually..
 But did you see that above logics don't work for every scenario to get the right answer, even the slowNext gives the incorrect result due to obvious reasons. And while writting the efficient logics we do make such mistakes, even I make such mistakes many times.
 Above are those examples. So what is correct way? It is given below which I think should give correct results, if you get it failing for any valid case then do let me know, I will appreciate that.
Picture
 Now you see the above way, do you think we can make it more efficient?
 Try & let me know, as I am looking to have good performance for huge numbers outside the range of Integer, Long, Double etc.
Powered by Create your own unique website with customizable templates.