How to make Print() method memory & CPU efficient? Problem Statement
You are receiving n objects in a random order, and you need to print them to stdout correctly ordered by sequence number.
The sequence numbers start from 0 (zero) and you have to wait until you get a complete, unbroken sequence batch of j objects before you output them.
You have to process all objects without loss. The program should exit once it completes outputting the first 50000 objects Batch size j = 100
The object is defined as such:
    {
    &quot;id&quot; : &quot;object_id&quot;, // object ID (string)
    &quot;seq&quot; : 0, // object sequence number (int64, 0-49999)
    &quot;data&quot; : &quot;&quot; // []bytes
    }

Example Output Statement
    Step                Input Value                Output State j = 1                  Output state j = 3
    0                       6
    1                       0                           0
    2                       4                           0
    3                       2                           0
    4                       1                           0,1,2                               0,1,2
    5                       3                           0,1,2,3,4                           0,1,2
    6                       9                           0,1,2,3,4                           0,1,2
    7                       5                           0,1,2,3,4,5,6                       0,1,2,3,4,5


Solution
func (receiver *Receiver) Print(seqNumber uint64, batchSize uint64, outputFile io.Writer) (error, bool) {

    fmt.Fprintf(outputFile, &quot;[ &quot;)
    if seqNumber &gt;= receiver.outputSequence.length {
        receiver.outputSequence.bufferSizeIncrease(seqNumber)
    }
    receiver.outputSequence.sequence[seqNumber] = true

    printedCount := uint64(0) // check for MAX_OBJECTS_TO_PRINT
    var nthBatchStartingIndex uint64
    MaxObjectsToPrint := config.GetMaxPrintSize()
Loop:
    for nthBatchStartingIndex &lt; receiver.outputSequence.length { // check unbroken sequence
        var assessIndex = nthBatchStartingIndex
        for j := assessIndex; j &lt; nthBatchStartingIndex+batchSize; j++ { // Assess nth batch
            if j &gt;= receiver.outputSequence.length { //index out of range - edge case
                break Loop
            }
            if receiver.outputSequence.sequence[j] == false {
                break Loop
            }
        }

        count, printThresholdReached := receiver.printAssessedBatchIndexes(assessIndex, printedCount, batchSize, MaxObjectsToPrint, outputFile)
        if printThresholdReached { // print sequence threshold reached MAX_OBJECTS_TO_PRINT
            fmt.Fprintf(outputFile, &quot; ]  &quot;)
            fmt.Fprintf(outputFile, &quot; ----for input value %d\n&quot;, seqNumber)
            return nil, false
        }
        printedCount += count
        if printedCount &gt;= MaxObjectsToPrint { // print sequence threshold reached MAX_OBJECTS_TO_PRINT
            fmt.Fprintf(outputFile, &quot; ]  &quot;)
            fmt.Fprintf(outputFile, &quot; ----for input value %d\n&quot;, seqNumber)
            receiver.Log.Printf(&quot;****MaxObjectsToPrint threshold(%d) reached \n&quot;, MaxObjectsToPrint)
            return nil, false
        }
        nthBatchStartingIndex = assessIndex + batchSize // next batch
    }
    fmt.Fprintf(outputFile, &quot; ]  &quot;)
    fmt.Fprintf(outputFile, &quot; ----for input value %d\n&quot;, seqNumber)
    return nil, true
}


Here is the complete solution, written for this problem.

Print() is the method that does heavy lifting in this code, with varying size of memory &amp; heavy CPU usage:

How to make receiver.outputSequence memory effective by using datastructure other than array? because newBufferSize := 2 * seqNumber is doubling memory...

How to make Print method have effective CPU usage? On some goroutine
First, observe that if you receive an out-of-sequence object, you have to potentially keep all the objects from the latest printed sequence number to the received number. Worst case, you receive the object in reverse order and have to keep them all in memory. There is no way around this. So, what you can optimize is first, when you are printing objects you can find them quickly, and second, you don't want to move them around, which will happen if you use a slice and grow it as necessary.
So, a sketch of an algorithm that will use CPU and memory better than what you have is as follows:
var nextInSequence=0 // The next item you are expecting in the sequence
var storedObjects=map[int]SomeStruct{}

func Print(seqNumber int, obj SomeStruct) {
   if seqNumber==nextInSequence {
     output obj
     nextInSequence++
   } else {
      storedObjects[seqNumber]=obj
   }
   for {
     if stored, ok:=storedObjects[nextInSequence] ; ok {
        output stored
        delete(storedObjects,nextInSequence)
        nextInSequence ++
     } else {
        break
     }
  }
}