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

Reverse the String in n/2 complexity

7/26/2024

0 Comments

 
I believe, everyone would have got this question or have asked during the interviews.
I came to this question again through one blog where he suggested to solve it via looping n/2 times like shown below -
Picture
And if you ask then response will be like-
You are iterating n/2 times, so runtime complexity is n/2

And my understanding is, you are not just counting the numbers, you are accessing the elements in the array. So have you reduced those operations.
So don't take count of iterations as complexity, first optimize the number of operations & their complexity then the count of iterations will matter.
If reducing the number of operations X iterations ~ number of operations X reduced ierations
then you may not be getting any benefit or any considerable benefit.

So you can try below function which runs n times-
Picture
Some may claim that for small or small number of Strings, there may not be any difference.
So I request you to run these function for large Strings & multiple times.
​I executed these functions for Strings of length 1,000,000 & executed each function for 100,000 times, to get the below results-
 reverse took - 225 Sec
 reverseEach took - 254 Sec

I think, generally you will not be running this, so many times or for so big Strings, so running n/2 times will not be giving much visible benefit. But running n/2 times is good when you do reverse an array in-place, without requiring a new array.
​For reversing the String, it is senseless.
​To get the best performance try Reverse String InPlace.

Below image shows the result when executed various functions to reverse the String or character array for Strings of length 1 million & executed for 100,000 times.

I believe, few point are uncovered & or there will be something to discuss, so please provide your comments to have a discussion around it & should it be asked like this in interviews?
Picture
0 Comments



Leave a Reply.

    Author

    Nitin Agrawal

    Categories

    All

    RSS Feed

Powered by Create your own unique website with customizable templates.